简体   繁体   English

在应用程序类中注册和取消注册BroadcastReceiver

[英]Register and Unregister the BroadcastReceiver in Application Class

I have a broadcast receiver Which is registered in the onCreate() method of Android Applcation class but How to unRegister the same 我有一个广播接收器,它在Android Applcation类的onCreate()方法中注册但是如何取消注册

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. 在上面的代码中,我在应用程序onCreate()方法中注册了它,并且Application类中没有onDestroy()/ onStop()方法来取消注册broadcastReceiver。

How to achieve it 如何实现它

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 ) https://developer.android.com/guide/components/broadcasts.html

you should create a BaseActivity. 你应该创建一个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 MainActivity扩展了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 你可以在Application类中调用unregister接收器,只需要像这样调用

in your MainActivity inside of onDesctroy() method call 在你的MainActivity里面的onDesctroy()方法调用

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

we create unregisterReceiver() method in your MyApplication class 我们在MyApplication类中创建unregisterReceiver()方法

 public class MyApplication extends Application {


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

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

从您要取消注册的位置调用此方法

unregisterReceiver();

Please use statically registered broadcast receivers in your manifest which doesn't require you to call registerReceiver() and unRegisterReceiver() 请在清单中使用静态注册的广播接收器,不要求您调用registerReceiver()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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM