gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-30 19:51:53 25 4
gpt4 key购买 nike

我有一个广播接收器,它已在 Android Applcation 类的 onCreate() 方法中注册,但如何取消注册它

例子

public class MyApplication extends Application {


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

在上面的代码中,我已经在应用程序的 onCreate() 方法中注册了它,并且在 Application 类中没有 onDestroy()/onStop() 方法来取消注册 broadcastReceiver。

如何实现

最佳答案

如果您想在应用程序运行的整个过程中收听,则无需注销。来自文档(截至今天):

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 )

关于java - 在应用程序类中注册和取消注册 BroadcastReceiver,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45728334/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com