how can i run broadcastreceiver without stoping? - android

I want to run broadcastreceiver without stoping and i dont know how.
I do not know if there is service runs all the time...
Thnaks!

You don't need to start/stop a BroadcastReceiver. Its not your background running service. All you need is to register or unregister it for your application. Once registered, its always ON.
When some specific event occur, system notifies(broadcast) all registered applications about the event. all registered apps receives this as an Intent. Also, you can send your own broadcast.
for more, see this
Simple Example:
in my manifest, I'm including a permission and a receiver
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.receivercall"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="10" />
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<receiver android:name=".Main">
<intent-filter >
<action android:name="android.intent.action.PHONE_STATE"/>
</intent-filter>
</receiver>
<activity android:name=".TelServices">
<intent-filter >
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
Now, my receiver, Main.java
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.TelephonyManager;
import android.widget.Toast;
public class Main extends BroadcastReceiver {
String number,state;
#Override
public void onReceive(Context context, Intent intent) {
state=intent.getStringExtra(TelephonyManager.EXTRA_STATE);
if(state.equals(TelephonyManager.EXTRA_STATE_RINGING)){
number=intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
Toast.makeText(context, "Call from : "+number, Toast.LENGTH_LONG).show();
}
else if(state.equals(TelephonyManager.EXTRA_STATE_IDLE))
Toast.makeText(context, "Call ended", Toast.LENGTH_LONG).show();
else
Toast.makeText(context, intent.getAction(), Toast.LENGTH_LONG).show();
}
}
Here, when I install this app, I'm registering a Broadcastreceiver through manifest. And the Toast will appear every time a call comes/ends.

Related

Logcat not working inside onReceive in Broadcast Receiver while listening to deskclock alarm intent

I am listening to deskclock alarm change intent using my broadcast receiver. When the onReceive() method in my Broadcast Receiver is called, the logs (Log.i/v()) inside the onReceive are not getting printed on Android monitor but Toasts are working just fine.
Manifest File :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.dumbrella.ratemyday">
<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:process=":remote" android:name="AlarmClockReceiver">
<intent-filter>
<action android:name="com.android.deskclock.ALARM_DISMISS" />
<action android:name="com.android.deskclock.ALARM_SNOOZE" />
<action android:name="com.android.deskclock.ALARM_ALERT" />
<action android:name="com.android.deskclock.ALARM_DONE" />
</intent-filter>
</receiver>
</application>
</manifest>
Broadcast Receiver:
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
import java.text.SimpleDateFormat;
public class AlarmClockReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// This log.v does not get printed on Android Monitor
Log.v("Broadcast Test", "In Broadcast Listener");
String message = "Broadcast intent detected "
+ intent.getAction();
// This toast gets displayed after the alarm is dismissed
Toast.makeText(context, message,Toast.LENGTH_LONG).show();
}
}
Maybe it's too late, but I leave it for future programmers with this problem, I had a similar problem and solved it this way.
Simply in Logcat panel, change the filtering listbox to "No Filters".
You might want to make sure that you have selected the correct device in your DDMS perspective, also ensure that you have selected the correct filtering option and log level - in your case you want to select "verbose". Also check out the selected answer and suggestions as to why Logcat is not displaying logs.
You can filter logs to show BroadcastReceiver logs in LogCat Filter Configuration section:
The complete solution steps: Logs/logcat not working in BroadcastReceiver

Android broadcast is not working when the app is not running

Please don`t vote down if this is a silly question please share some light here
i want to created an app for saving call history
for that i followed
http://karanbalkar.com/2014/02/detect-incoming-call-and-call-hangup-event-in-android/
this site
but i`m not getting the event when the app is not running.Please help
i want to get the event when a incoming call is received even when the app is closed
manifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sars.broadcasttest">
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme">
<activity android:name=".Main">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".IncommingCall">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
</application>
</manifest>
class file
package com.sars.broadcasttest;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
/**
* Created by athulbabu on 18/08/15.
*/
public class IncommingCall extends BroadcastReceiver {
int i=0;
#Override
public void onReceive(Context context, Intent intent) {
Log.d("IncomingCall", "Call catched test"+i++);
Toast.makeText(context, "Calll Intent Detected. test", Toast.LENGTH_LONG).show();
}
}
For security reasons you have to run app at least once after install to get the BroadcastReceiver working.
Read this section: Launch controls on stopped applications
http://developer.android.com/about/versions/android-3.1.html

How does a Android "OS" detect a incoming call

I'd like to know:
how the android OS detect a incoming call(number) and displays the contact name and gives us a option to attend the call.
What happens inside the OS when the "END CALL BUTTON" is tapped.
When I searched regarding this I am getting only the Classes and methods to create my own app. Requesting for the explanation.
In Android it is possible to detect call events using the built-in TelephonyManager API.TelephonyManager class provides access to information about the telephony services on the device.
Example :
Create a new class called MyCallReceiver
package com.example;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.TelephonyManager;
import android.widget.Toast;
public class MyCallReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_RINGING)) {
// This code will execute when the phone has an incoming call
// get the phone number
String incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
Toast.makeText(context, "Call from:" +incomingNumber, Toast.LENGTH_LONG).show();
} else if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
TelephonyManager.EXTRA_STATE_IDLE)
|| intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
TelephonyManager.EXTRA_STATE_OFFHOOK)) {
// This code will execute when the call is disconnected
Toast.makeText(context, "Detected call hangup event", Toast.LENGTH_LONG).show();
}
}
}
BroadcastReceiver class that will monitor the phone state and whenever there is a change in phone state, the onReceive() method of the BroadcastReceiver will be called.
Add the READ_PHONE_STATE permission in your AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="com.example.MyCallReceiver" >
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
</application>
</manifest>
Check this for references : BroadcastReceiver

How to properly auto-start APP

i'm having trouble with the auto-start feature of my APP.
I've searched trhough the forums, seen many suggestions, none seems to work and I don't know why.
Here's the BootUpReciver.java
package com.???.???;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
public class BootUpReciver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
//Intent pushIntent = new Intent(context, BackgroundService.class);
//context.startService(pushIntent);
Toast.makeText(context, "Device Booted", Toast.LENGTH_LONG).show();
Log.d("TAG","Device Booted");
}
}
}
And here's my Manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.???.???"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="17" />
<application
android:theme="#android:style/Theme.Holo"
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<receiver android:name=".BootUpReciver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
<activity
android:name="com.???.???.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
When i restart my phone i don't even see the Toast, why? what should i do? I need to auto-start to get some sharedprefs and write a value to a file every boot.
Thanks.
While testing I found that If I show toast in onReceive then it is not showing. But if I start an activity in onReceive then it is working. So you can also check this. Start an activity or service and perform your task there.

Android App Not Responding the SMS Receiver

I have been working on an app to respond to received SMS messages (before you complain, I know this has been asked A LOT, but believe me, I can't get it to work at all, and I've tried searching for hours). I've got my manifest set up like so:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.csbctech.notiscreen"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".NotiScreenActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="NotiScreenSmsReceiver" android:process=":remote">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
</application>
</manifest>
And my receiver class looks like this:
package com.csbctech.notiscreen;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.CountDownTimer;
import android.os.PowerManager;
import android.util.Log;
public class NotiScreenSmsReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Log.w("NotiScreen", "Got SMS");
}
}
But the "NotiScreenSmsReceiver" class never gets called. I've tried several different examples, and I can't for the life of me get the receiver class to get called...I've even tried removing the uses-permission, and I don't even get an error message about not having permissions. What could be wrong? Oh please help me, you're my only hope!
Are you running this on a phone with a custom SMS app such as GOSMS? I've heard that some of those programs will stop the broadcast so that they can create their own notifications and stop the stock ones.
Change
<receiver android:name="NotiScreenSmsReceiver" android:process=":remote">
to
<receiver android:name=".NotiScreenSmsReceiver" android:process=":remote">

Categories

Resources