Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to unregister BroadcastReceiver

My app uses a BroadcastReceiver to get sms in this way:

SmsBR.java

public class SmsBR extends BroadcastReceiver {      @Override     public void onReceive(Context context, Intent intent) {         Bundle bundle = intent.getExtras();         if (bundle != null) {             Object[] pdus = (Object[])bundle.get("pdus");             final SmsMessage[] messages = new SmsMessage[pdus.length];              for (int i = 0; i < pdus.length; i++) {                 messages[i] = SmsMessage.createFromPdu((byte[])pdus[i]);             }             if (messages.length > 0)                 //doSomething();             }         }     } } 

Manifest.xml

<application android:icon="@drawable/icon" android:label="@string/app_name">     <activity android:name=".Activity" 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=".SmsBR">         <intent-filter>             <action android:name="android.provider.Telephony.SMS_RECEIVED"></action>         </intent-filter>     </receiver> </application>     

In this way SmsBR is always ON. I want to register it when a service starts (onCreate()) and unregister it when the service is destroyed (onDestroy()). How can I do this?

like image 832
supergiox Avatar asked Sep 16 '11 00:09

supergiox


People also ask

Do I need to unregister BroadcastReceiver?

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

How do I unregister a broadcast receiver?

Use unregisterReceiver(BroadcastReceiver receiver) in your onPause() to unregister the Broadcast receiver. For a Service: Remove the receiver tag from the manifest file. You can then register your Broadcast receiver with the same method in the onCreate() and unregister in the onDestroy() .

Where we should register and deregister the broadcast?

You should register and unregister your broadcast in onResume() and onPause() methods. if you register in onStart() and unregister it in onStop().

How do I know if my broadcast receiver is registered?

A simple solution to this problem is to call the registerReceiver() in your Custom Application Class. This will ensure that your Broadcast receiver will be called only one in your entire Application lifecycle.


1 Answers

Edit:

For an Activity:

In order to register your broadcast receiver from within your app, first, remove the <receiver> tag from your AndroidManifest.xml file. Then, call registerReceiver(BroadcastReceiver receiver, IntentFilter filter) in your onResume(). Use unregisterReceiver(BroadcastReceiver receiver) in your onPause() to unregister the Broadcast receiver.

For a Service:

Remove the receiver tag from the manifest file. You can then register your Broadcast receiver with the same method in the onCreate() and unregister in the onDestroy().

EDIT: Sample Code:

public class MyActivity extends Activity {   private final BroadcastReceiver mybroadcast = new SmsBR();    public void onResume() {     super.onResume();      IntentFilter filter = new IntentFilter();     filter.addAction("android.provider.Telephony.SMS_RECEIVED");     registerReceiver(mybroadcast, filter);   }    public void onPause() {     super.onPause();      unregisterReceiver(mybroadcast);   } } 
like image 141
WindsurferOak Avatar answered Sep 19 '22 20:09

WindsurferOak