Why BroadcastReceiver works even when app is in background ?

后端 未结 9 909
感动是毒
感动是毒 2020-12-04 13:59

I am checking Internet connectivity in my app using BroadcastReceiver and I show an alert dialog if the connection is lost. It works fine. But my problem is that BroadcastRe

9条回答
  •  眼角桃花
    2020-12-04 14:11

    Have you tried to remove the Intent filter from the manifest and register/unregister it in activity? So you can try to register Intent filter in onStart() and unregister it on onStop() methods. The code goes somethink like this:

    static final String ACTION = "android.net.conn.CONNECTIVITY_CHANGE";
    
    
    IntentFilter filter = new IntentFilter(ACTION);
    this.registerReceiver(ConnectivityChangedReceiver, filter);
    
    
    unregisterReceiver(ConnectivityChangedReceiver);
    

    You should also learn about Activity Lifecycle, if it's not familiar yet.

提交回复
热议问题