Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android BroadcastReceiver on startup - keep running when Activity is in Background

I'm monitoring incoming SMSs.

My app is working perfectly with a BroadcastReceiver. However it is working from an Activity and would like to keep the BroadcastReceiver running all the time (and not just when my Activity is running).

How can I achieve this? I've looked through the lifecycle of the BroadcastReceiver but all that is mentioned in the documentation is that the lifecycle is limited to the onReceive method, not the lifecycle of keeping the BroadcastReceiver checking for incoming SMS.

How can I make this persistent?

Thanks

like image 391
RasberryPoptart Avatar asked Mar 13 '11 14:03

RasberryPoptart


People also ask

How do I turn off BroadcastReceiver on Android?

To stop receiving broadcasts, call unregisterReceiver(android. content. BroadcastReceiver) . Be sure to unregister the receiver when you no longer need it or the context is no longer valid.

Does broadcast receiver work in background?

A broadcast receiver will always get notified of a broadcast, regardless of the status of your application. It doesn't matter if your application is currently running, in the background or not running at all.

What is BroadcastReceiver Android?

A broadcast receiver (receiver) is an Android component which allows you to register for system or application events. All registered receivers for an event are notified by the Android runtime once this event happens.

Is it necessary to unregister BroadcastReceiver?

It's always suggested to register and unregister broadcast receiver programmatically as it saves system resources.


2 Answers

You need to define a receiver in manifest with action name android.intent.action.BOOT_COMPLETED.

<!-- Start the Service if applicable on boot --> <receiver android:name="com.prac.test.ServiceStarter">     <intent-filter>         <action android:name="android.intent.action.BOOT_COMPLETED"/>     </intent-filter> </receiver> 

Make sure also to include the completed boot permission.

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/> 

Use Service for this to make anything persist. And use receivers to receive Boot Up events to restart the service again if system boots..

Code for Starting Service on boot up. Make Service do your work of checking sms or whatever you want. You need to do your work in MyPersistingService define it your self.

import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.util.Log;  public class ServiceStarter extends BroadcastReceiver {      @Override     public void onReceive(Context context, Intent intent) {         Intent i = new Intent("com.prac.test.MyPersistingService");         i.setClass(context, MyPersistingService.class);         context.startService(i);     } } 
like image 147
Rohit Sharma Avatar answered Sep 24 '22 16:09

Rohit Sharma


Service or Boot Completed is not mandatory

In fact, you don't need to implement a Service or register to android.intent.action.BOOT_COMPLETED

Some examples shows how to register/unregister a BroadcastReceiver when activity is created and destroyed. However, this is useful for intents that you expect only when app is opened (for internal communication between Service/Activity for example).

However, in case of a SMS, you want to listen to the intent all the time (and not only when you app is opened).

There's another way

You can create a class which extends BroadcastReceiver and register to desired intents via AndroidManifest.xml. This way, the BroadcastReceiver will be indepedent from your Activity (and will not depend from Activity's Life Cycle)

This way, your BroadcastReceiver will be notified automatically by Android as soon as an SMS arrive even if your app is closed.

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?> <manifest>     ...     <uses-permission android:name="android.permission.READ_SMS"/>     <uses-permission android:name="android.permission.RECEIVE_SMS"/>      <application>         ....         <receiver android:name=".MyCustomBroadcastReceiver">             <intent-filter>                 <action android:name="android.provider.Telephony.SMS_RECEIVED" />             </intent-filter>         </receiver>     </application> </manifest> 

MyCustomBroadcastReceiver.java

public class MyCustomBroadcastReceiver extends BroadcastReceiver {      @Override     public void onReceive(Context context, Intent intent) {         if(intent != null) {             String action = intent.getAction();             if(action != null) {                 if(action.equals("android.provider.Telephony.SMS_RECEIVED")) {                     // DO YOUR STUFF                 } else if (action.equals("ANOTHER ACTION")) {                     // DO ANOTHER STUFF                 }             }         }     } } 

Notes

You can add others intent-filters to AndroidManifest and handle all of them in same BroadcastReceiver.

Start a Service only if you will perform a long task. You just need to display a notification or update some database, just use the code above.

like image 29
W0rmH0le Avatar answered Sep 26 '22 16:09

W0rmH0le