Vikramsinh Gaikwad
Vikramsinh Gaikwad

Reputation: 911

How to unregister broadcast receiver from an Application Class?

I register broadcast receiver in Android's Application Class,but now my question is where to unregister that broadcast ?

public class MyApplication extends Application 

{

@Override
public void onCreate() {
     ..... 
     // OTHER INITIALIZATIONS
     initNetworkBR();
} 
private void initNetworkBR() {

      br = new BroadcastReceiver() {
        ....
      };
      IntentFilter filter = new IntentFilter();
      filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
      registerReceiver(br, filter);
   }
}

Upvotes: 2

Views: 4229

Answers (7)

Bharat Lalwani
Bharat Lalwani

Reputation: 1530

As @Naruto Uzumaki mentioned above you can deregister it on the onDestroy method of Application. But it's always a better option to use Broadcast Receiversin Activity instead of whole Application.

You can use the below approach to use it in Activity:-

public class DetailsActivity {

private BroadcastReceiver mNetworkReceiver;

 @Override
    protected void onCreate(Bundle savedInstanceState) {
    mNetworkReceiver = new NetworkReceiver();
    registerReceiver(mNetworkReceiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
 }

Here is the way to unregister it.

 @Override
 protected void onDestroy() {
    super.onDestroy();
    unregisterNetworkChanges();
 }

 protected void unregisterNetworkChanges() {
    try {
        unregisterReceiver(mNetworkReceiver);
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    }
  }

Upvotes: 1

I use Application.ActivityLifecycleCallbacks, that listen all callbacks from activities lifecycle. It can replace the suggestion to use a Base Activity using inheritance and in other hand use composition to resolve this problem.

You implement Application.ActivityLifecycleCallbacks interface in a class and in on create from your application class register the callback as continue:

registerActivityLifecycleCallbacks(<here your typed class with ActivityLifecycleCallbacks>)

Then, you have full control over al activities lifecycle to do something like unregister or register receivers.

I hope it can be helpful

Upvotes: 1

Samir Bhatt
Samir Bhatt

Reputation: 3261

Try with below code :
if you have registered from Manifiest.xml

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

    int status = context.getPackageManager().getComponentEnabledSetting(component);
    if(status == PackageManager.COMPONENT_ENABLED_STATE_ENABLED) {
     context.getPackageManager().setComponentEnabledSetting(component, PackageManager.COMPONENT_ENABLED_STATE_DISABLED , PackageManager.DONT_KILL_APP);
    }

If programmatically registering reciever :

@Override
public void onCreate(Bundle savedInstanceState) {

  LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver,
      new IntentFilter("custom-event-name"));
}

@Override
protected void onDestroy() {
  // Unregister since the activity is about to be closed.
  LocalBroadcastManager.getInstance(this).unregisterReceiver(mMessageReceiver);
  super.onDestroy();
}

Upvotes: 0

Naruto Uzumaki
Naruto Uzumaki

Reputation: 2087

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
}

Upvotes: 3

Dhruv Patel
Dhruv Patel

Reputation: 1529

I think you should use LocalBroadcastManager to register and unregister receiver instead of BroadcastReceiver.

Benefits to use LocalBroadcastManager

@Override
public void onCreate(Bundle savedInstanceState) {

  LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver,
      new IntentFilter("custom-event-name"));
}

@Override
protected void onDestroy() {
  // Unregister since the activity is about to be closed.
  LocalBroadcastManager.getInstance(this).unregisterReceiver(mMessageReceiver);
  super.onDestroy();
}

Upvotes: 0

SpiritCrusher
SpiritCrusher

Reputation: 21063

Except using Broadcast Receiver in Application class you can Use a Base class for all your Activities. And register BR in the Base Activity then provide call backs to the childs. As Activity has a strict lifecycle model so its better to use Runtime Broadcast receivers in Activity rather than Application class .

 public class BaseActivity extends AppCompatActivity {
    @Override
    protected void onResume() {
        super.onResume();
        //registerReceiver Here
    }

    @Override
    protected void onPause() {
        super.onPause();
        //unregisterReceiver here
    }
}

Upvotes: 1

jorjSB
jorjSB

Reputation: 610

You should use unregisterReceiver(BroadcastReceiver receiver) in your onPause() to unregister the Broadcast receiver.

Example:

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

Upvotes: 0

Related Questions