4

I have a BroadcastReceiver that checks for network connection declared statically as:

<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?

If it is not possible in Application class, do I have to manually unregister it everywhere in onStop()? and register it in onStart() in every activity?

CopsOnRoad
  • 237,138
  • 77
  • 654
  • 440
Ackman
  • 1,562
  • 6
  • 31
  • 54

2 Answers2

3

You expect that registering and unregistering BroadcastReceiver on every activity。 I think the application class is the best solution。 there is a function in Application class which named registerActivityLifecycleCallbacks(), you can watch your activities lifecycle callback in real time.

CopsOnRoad
  • 237,138
  • 77
  • 654
  • 440
  • Hi :) so do you suggest that for every activity this callback will be called again and again? Also when every activity in my application calls onstop or ondestroy unregister will be called? – Ackman Mar 30 '17 at 16:59
  • @Ackman:Yes,the callback will be called again and again,But you can control your code flow logic.for example,your application increate counter when onActivityStarted() was call, if counter is equal 1,register the broadcast receiver; decrease your counter when onActivityStoped() was called, if the counter is equal 0, then unregister the broadcast receiver. do you understand?may be a code demo looks like more helpful. ^_^ – lookingforhu Apr 01 '17 at 01:07
2

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.

(at least until Android O is released, but that is a story for another time)

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()

  • If it is while some particular service is running, use registerReceiver() in the service's onCreate() and unregisterReceiver() in onDestroy()

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Okay so if i start a service and register and unregister my broadcast receiver in that service. Will the service be stopped or killed once my app is killed manually? – Ackman Mar 29 '17 at 23:54
  • in other words what is the life time of a service in android? Do I just have to start it just once anywhere in the application and it will continue to live unless the app gets killed? – Ackman Mar 29 '17 at 23:56
  • @Ackman: To some extent, [you control the lifetime of the service](https://developer.android.com/guide/components/services.html#Lifecycle). However, that is somewhat secondary. You created your `NetworkChangeReceiver`. Why? What is the point of this receiver? What are the business rules for what this receiver should be doing? You need to work out the desired app functionality, **then** decide what components should be used, and how they should be used. – CommonsWare Mar 30 '17 at 00:00
  • Basic idea is that whenever my network changes i.e. wifi off or cellular signal gone. A new activity "service unavailable" is called. I think registering and unregistering broadcast receivers on every activity would be great solution. – Ackman Mar 30 '17 at 00:08