广播接收器即使在应用程序被用户杀死后也能正常工作
Broadcast receiver working even after app is killed by user
我有一个 BroadcastReceiver
检查网络连接静态声明为:
<receiver
android:name=".core.util.NetworkChangeReceiver"
android:label="NetworkChangeReceiver">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
<action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
</intent-filter>
</receiver>
网络更改接收器执行如下操作:
public class NetworkChangeReceiver extends BroadcastReceiver {
@Override
public void onReceive(final Context context, final Intent intent) {
String status = NetworkUtil.getConnectivityStatusString(context);
if(status == "Not connected to Internet") {
Intent i = new Intent(context, Dialog.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
}
问题是当我手动终止我的应用程序并关闭网络(飞行模式)时,对话 activity 启动。这怎么可能?我的广播接收器还在后台运行吗?
问题:
有没有办法在应用程序被终止后取消注册我的广播接收器?也许在 Android 的应用程序单例 class 中?
如果在应用程序 class 中不可行,我是否必须在 onStop()
中的所有地方手动取消注册?并在每个 activity?
的 onStart()
中注册它
How is this possible?
您在清单中注册了广播。如果发出匹配的广播,您的应用程序将响应。如果需要,Android 会为您分叉一个进程,如果您的应用当时不是 运行。
(至少在 Android O 发布之前,但那是另一个故事)
is there a way for me to unregister my broadcast receiver once the application is killed?
您需要决定您想要接收此广播的时间段:
如果某些特定的 activity 在前台,请使用 registerReceiver()
而不是清单,在 activity 中注册广播onStart()
方法并在 onStop()
中注销
如果某些特定服务是 运行,请在服务的 onCreate()
中使用 registerReceiver()
并在 onDestroy()
中使用 unregisterReceiver()
您希望在每个 activity 上注册和注销 BroadcastReceiver
。
我认为 application class 是最好的解决方案。
Application
class 中有一个名为 registerActivityLifecycleCallbacks() 的函数,
您可以实时观看您的活动生命周期回调。
我有一个 BroadcastReceiver
检查网络连接静态声明为:
<receiver
android:name=".core.util.NetworkChangeReceiver"
android:label="NetworkChangeReceiver">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
<action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
</intent-filter>
</receiver>
网络更改接收器执行如下操作:
public class NetworkChangeReceiver extends BroadcastReceiver {
@Override
public void onReceive(final Context context, final Intent intent) {
String status = NetworkUtil.getConnectivityStatusString(context);
if(status == "Not connected to Internet") {
Intent i = new Intent(context, Dialog.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
}
问题是当我手动终止我的应用程序并关闭网络(飞行模式)时,对话 activity 启动。这怎么可能?我的广播接收器还在后台运行吗?
问题:
有没有办法在应用程序被终止后取消注册我的广播接收器?也许在 Android 的应用程序单例 class 中?
如果在应用程序 class 中不可行,我是否必须在 onStop()
中的所有地方手动取消注册?并在每个 activity?
onStart()
中注册它
How is this possible?
您在清单中注册了广播。如果发出匹配的广播,您的应用程序将响应。如果需要,Android 会为您分叉一个进程,如果您的应用当时不是 运行。
(至少在 Android O 发布之前,但那是另一个故事)
is there a way for me to unregister my broadcast receiver once the application is killed?
您需要决定您想要接收此广播的时间段:
如果某些特定的 activity 在前台,请使用
registerReceiver()
而不是清单,在 activity 中注册广播onStart()
方法并在onStop()
中注销
如果某些特定服务是 运行,请在服务的
onCreate()
中使用registerReceiver()
并在onDestroy()
中使用unregisterReceiver()
您希望在每个 activity 上注册和注销 BroadcastReceiver
。
我认为 application class 是最好的解决方案。
Application
class 中有一个名为 registerActivityLifecycleCallbacks() 的函数,
您可以实时观看您的活动生命周期回调。