简体   繁体   English

广播接收器即使在app被用户杀死后仍然工作

[英]Broadcast receiver working even after app is killed by user

I have a BroadcastReceiver that checks for network connection declared statically as: 我有一个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>

Network change receiver does something like: 网络变更接收器做的事情如下:

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);
    }
}

} }

The problem is that when I manually kill my app and turn off network (airplane mode), the Dialogue activity launches. 问题是,当我手动终止我的应用程序并关闭网络(飞行模式)时,会启动对话活动。 How is this possible? 这怎么可能? Is my broadcast receiver still alive in the background? 我的广播接收器在后台还活着吗?

Question: 题:

is there a way for me to unregister my broadcast receiver once the application is killed? 一旦应用程序被杀,我有办法取消注册我的广播接收器吗? maybe in the application singleton class of Android? 也许在应用程序的单例类Android?

If it is not possible in Application class, do I have to manually unregister it everywhere in onStop() ? 如果在Application类中不可能,我是否必须在onStop()手动取消注册它? and register it in onStart() in every activity? 并在每个活动的onStart()中注册它?

您希望在每个活动上注册和取消注册BroadcastReceiver 。我认为应用程序类是最好的解决方案Application类中有一个名为registerActivityLifecycleCallbacks ()的函数,您可以实时查看您的活动生命周期回调。

How is this possible? 这怎么可能?

You registered for the broadcast in the manifest. 您在清单中注册了广播。 If a matching broadcast is sent out, your app will respond. 如果发送了匹配的广播,您的应用将会响应。 If needed, Android will fork a process for you, if your app is not running at the time. 如果需要,如果您的应用当时没有运行,Android将为您分配一个流程。

(at least until Android O is released, but that is a story for another time) (至少在Android O发布之前,但这是另一个时间的故事)

is there a way for me to unregister my broadcast receiver once the application is killed? 一旦应用程序被杀,我有办法取消注册我的广播接收器吗?

You need to decide the period of time you want to receive this broadcast: 您需要决定接收此广播的时间段:

  • If it is while some particular activity is in the foreground, use registerReceiver() instead of the manifest, registering for the broadcast in the activity's onStart() method and unregistering in onStop() 如果某个特定活动位于前台,请使用registerReceiver()而不是清单,在活动的onStart()方法中注册广播并在onStop()取消注册

  • If it is while some particular service is running, use registerReceiver() in the service's onCreate() and unregisterReceiver() in onDestroy() 如果某个特定服务正在运行,请在服务的onCreate()使用registerReceiver() ,在onDestroy() unregisterReceiver() onDestroy()

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

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