26

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

Anbu
  • 671
  • 1
  • 6
  • 18
  • 2
    please check: Application.ActivityLifecycleCallbacks : link : https://developer.android.com/reference/android/app/Application.ActivityLifecycleCallbacks.html – Bapusaheb Shinde Aug 17 '17 at 06:56

5 Answers5

61

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)

Brian Yencho
  • 2,748
  • 1
  • 18
  • 19
5

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);
}
}
Nguyễn Trung Hiếu
  • 2,004
  • 1
  • 10
  • 22
  • 1
    Thanks, I am doing it but The Unregistration happens each and every time when I kill the I need to make the app listening even when it is in background – Anbu Aug 28 '17 at 10:45
2

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);
}
Mohit Suthar
  • 8,725
  • 10
  • 37
  • 67
  • 1
    This is fine but I have to show the Alert Dialog in when the TimeZone Changed Where i need to pass Activity Context to it so How I will do it – Anbu Aug 28 '17 at 10:42
  • First, you need to update your question, what exactly your problem – Mohit Suthar Aug 28 '17 at 12:10
-2

call this from where you want to unregister

unregisterReceiver();
Anshul..
  • 66
  • 1
  • 1
  • 10
-2

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>
Girish H
  • 69
  • 5
  • 5
    This will not work in Android API 26+: https://developer.android.com/about/versions/oreo/background.html#broadcasts – Mira_Cole Apr 05 '18 at 14:03