java – Register and Unregister the BroadcastReceiver in Application Class-ThrowExceptions

Exception or error:

I have a broadcast receiver Which is registered in the onCreate() method of Android Applcation class but How to unRegister the same

example

public class MyApplication extends Application {


@Override
public void onCreate() {
    super.onCreate();
    registerReceiver(broadcastReceiver, new IntentFilter("TIMEZONE_CHANGED"));
}

In the above code I have registered it in the application onCreate() method and there is no onDestroy() / onStop() method in the Application class to unregister the broadcastReceiver.

How to achieve it

How to solve:

You don’t need to unregister if you’d like to listen for the entire time the app is running. From the docs (as of today):

Context-registered receivers receive broadcasts as long as their registering context is valid. For an example, if you register within an Activity context, you receive broadcasts as long as the activity is not destroyed. If you register with the Application context, you receive broadcasts as long as the app is running.

(https://developer.android.com/guide/components/broadcasts.html)

###

you should create a BaseActivity.

Example

public class BaseActivity extends AppCompatActivity {

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    registerReceiver(broadcastReceiver, new IntentFilter("TIMEZONE_CHANGED"));
}

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

And MainActivity extend BaseActivity

example:

public class MainActivity extends BaseActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}
}

###

You can call unregister receiver inside Application class, only you just call like this

in your MainActivity inside of onDesctroy() method call

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

we create unregisterReceiver() method in your MyApplication class

 public class MyApplication extends Application {


    @Override
    public void onCreate() {
        super.onCreate();
        registerReceiver(broadcastReceiver, new IntentFilter("TIMEZONE_CHANGED"));
    }

public void unregisterReceiver() {
     unregisterReceiver(broadcastReceiver);
}

###

call this from where you want to unregister

unregisterReceiver();

###

Please use statically registered broadcast receivers in your manifest which doesn’t require you to call registerReceiver() and unRegisterReceiver()

<receiver
   android:name=".MyTimeChangeReceiver">
   <intent-filter>
        <action android:name="android.intent.action.TIMEZONE_CHANGED" />
        <action android:name="android.intent.action.TIME_SET" />
    </intent-filter>
</receiver>

Leave a Reply

Your email address will not be published. Required fields are marked *