How to Unregister Broadcastreceiver

How to unregister BroadcastReceiver

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);
}
}

How to unregister broadcast receiver from an Application Class?

One suggestion is in your launcher activity or desired activity unregister that like this:

@Override
protected void onDestroy() {
super.onDestroy();
((MyApplication) getApplication()).unregisterReceiver();
}

And implement unregisterReceiver method in MyApplication class:

public void unregisterReceiver() {
unregisterReceiver(br);//your broadcast
}

How to unregister BroadcastReceiver from AppWidgetProvider?

Solution:
Answer. Yes, registering a BroadcastReceiver in onUpdate(), will register a new one every time. I solved the issue of creating too many Broadcastreceivers by creating one in OnEnabled(), as that code only runs when the first widget is created.

Here's how I'm planning to unregister the broadcastReceiver I created in onEnabled. I will add the intent filter:ACTION_APPWIDGET_DISABLED to the broadcastReceiver when registering it. In the OnReceive() method of the custom broadcastReceiver I will execute:

@Override
public void onReceive(Context context, Intent intent){
if(intent.getAction().equals("ACTION_APPWIDGET_DISABLED"){
context.unregisterReceiver(this);
}
else{
...
//Rest of my method
{

{

When to unregister BroadcastReceiver? In onPause(), onDestroy(), or onStop()?

it depends on where you have register the receiver. The complementary method pairs are

onCreate - onDestroy
onResume - onPause
onStart - onStop

if you register the receiver in the first one then unregister it in it's ending method.

How to unregister a broadcast receiver properly

Unregistering the broadcast receiver is simple, you call unregisterReceiver method and pass the broadcast receiver to it.

Based on the answer you've shared

1) Change the registering of broadcast receiver in the onCreate method to

registerReceiver(gpsReceiver, new IntentFilter(LocationManager.PROVIDERS_CHANGED_ACTION));

2) Declare a global variable

final BroadcastReceiver gpsReceiver = new GpsReceiver(new LocationCallBack() {
@Override
public void onLocationTriggered() {
//Location state changed
}
});

3) In the onDestroy, Unregister the broadcast receiver

unregisterReceiver(gpsReceiver);

Android - how to unregister a receiver created in the manifest?

You can use the PackageManager to enable/disable a BroadcastReceiver in declared in the Manifest. The Broadcast Receiver will get fired only when it is enabled.

Use this to create a Component

ComponentName component = new ComponentName(context, MyReceiver.class);

Check if the Component is enabled or disabled

int status = context.getPackageManager().getComponentEnabledSetting(component);
if(status == PackageManager.COMPONENT_ENABLED_STATE_ENABLED) {
Log.d("receiver is enabled");
} else if(status == PackageManager.COMPONENT_ENABLED_STATE_DISABLED) {
Log.d("receiver is disabled");
}

Enable/Disable the component(Broadcast Receiver in your case)

//Disable
context.getPackageManager().setComponentEnabledSetting(component, PackageManager.COMPONENT_ENABLED_STATE_DISABLED , PackageManager.DONT_KILL_APP);
//Enable
context.getPackageManager().setComponentEnabledSetting(component, PackageManager.COMPONENT_ENABLED_STATE_ENABLED , PackageManager.DONT_KILL_APP);

How to unregister the broadcast receiver in android

You can call
unregisterReceiver (BroadcastReceiver receiver) to unregister the broadcast receivers. The context you have received as part of sendSMS should be stored and then later the same can be used to unregister the receiver.

EDIT:

private static Context mContext = null;

static BroadcastReceiver mSentReceiver = new BroadcastReceiver(){
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode()) {
case Activity.RESULT_OK:
Toast.makeText(mContext, "SMS sent",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(mContext, "Generic failure",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
Toast.makeText(mContext, "No cellular network service Available",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
Toast.makeText(mContext, "Null PDU",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
Toast.makeText(mContext, "Radio off",
Toast.LENGTH_SHORT).show();
break;
}
}

};

static BroadcastReceiver mDeleiveredReceiver = new BroadcastReceiver(){
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode()) {
case Activity.RESULT_OK:
Toast.makeText(mContext, "SMS delivered",
Toast.LENGTH_SHORT).show();
break;
case Activity.RESULT_CANCELED:
Toast.makeText(mContext, "SMS not delivered",
Toast.LENGTH_SHORT).show();
break;
}
}
};

public static void sendSMS(final Context context,String phoneNumber, String message) {
if(mContext == null) {
mContext = context;
mContext.registerReceiver(mSentReceiver, new IntentFilter(SENT));
mContext.registerReceiver(mDeleiveredReceiver, new IntentFilter(DELIVERED));
}

// rest of your code ....
}

// this method should be called whenever you want to remove the receivers
public static void removeReceivers() {
mContext.unregisterReceiver(mSentReceiver);
mContext.unregisterReceiver(mDeleiveredReceiver);
mContext = null;
}

Registering and unregistering BroadcastReceiver in a fragment

Have a look at life-cycle of Fragments:

onCreateView():
The system calls this when it's time for the fragment to draw its user interface for the first time. To draw a UI for your fragment, you must return a View from this method that is the root of your fragment's layout. You can return null if the fragment does not provide a UI.

onResume():
The fragment is visible in the running activity

onPause():
This is usually where you should commit any changes that should be persisted beyond the current user session (because the user might not come back).

Conclusion:

So it is better to register the receiver only inside onResume() and unregister inside onPause() because onCreateView() deals with view hierarchy only. It has nothing to do with receiver. So it is not harmful but surely it is useless.

I hope it will be helpful!!



Related Topics



Leave a reply



Submit