Android O – Detect connectivity change in background

Second edit: I’m now using firebase’s JobDispatcher and it works perfect across all platforms (thanks @cutiko). This is the basic structure:

public class ConnectivityJob extends JobService{

    @Override
    public boolean onStartJob(JobParameters job) {
        LogFactory.writeMessage(this, LOG_TAG, "Job created");
        connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            connectivityManager.registerNetworkCallback(new NetworkRequest.Builder().build(), networkCallback = new ConnectivityManager.NetworkCallback(){
                // -Snip-
            });
        }else{
            registerReceiver(connectivityChange = new BroadcastReceiver() {
                @Override
                public void onReceive(Context context, Intent intent) {
                    handleConnectivityChange(!intent.hasExtra("noConnectivity"), intent.getIntExtra("networkType", -1));
                }
            }, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
        }

        NetworkInfo activeNetwork = connectivityManager.getActiveNetworkInfo();
        if (activeNetwork == null) {
            LogFactory.writeMessage(this, LOG_TAG, "No active network.");
        }else{
            // Some logic..
        }
        LogFactory.writeMessage(this, LOG_TAG, "Done with onStartJob");
        return true;
    }


    @Override
    public boolean onStopJob(JobParameters job) {
        if(networkCallback != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)connectivityManager.unregisterNetworkCallback(networkCallback);
        else if(connectivityChange != null)unregisterReceiver(connectivityChange);
        return true;
    }

    private void handleConnectivityChange(NetworkInfo networkInfo){
        // Calls handleConnectivityChange(boolean connected, int type)
    }

    private void handleConnectivityChange(boolean connected, int type){
        // Calls handleConnectivityChange(boolean connected, ConnectionType connectionType)
    }

    private void handleConnectivityChange(boolean connected, ConnectionType connectionType){
        // Logic based on the new connection
    }

    private enum ConnectionType{
        MOBILE,WIFI,VPN,OTHER;
    }
}

I call it like so (in my boot receiver):

Job job = dispatcher.newJobBuilder()
                    .setService(ConnectivityJob.class)
                    .setTag("connectivity-job")
                    .setLifetime(Lifetime.FOREVER)
                    .setRetryStrategy(RetryStrategy.DEFAULT_LINEAR)
                    .setRecurring(true)
                    .setReplaceCurrent(true)
                    .setTrigger(Trigger.executionWindow(0, 0))
                    .build();

Edit: I found a hacky way. Really hacky to say. It works but I wouldn’t use it:

  • Start a foreground service, go into foreground mode using startForeground(id, notification) and use stopForeground after that, the user won’t see the notification but Android registers it as having been in the foreground
  • Start a second service, using startService
  • Stop the first service
  • Result: Congratulations, you have a service running in the background (the one you started second).
  • onTaskRemoved get’s called on the second service when you open the app and it is cleared from RAM, but not when the first service terminates. If you have an repeating action like a handler and don’t unregister it in onTaskRemoved it continues to run.

Effectively this starts a foreground service, which starts a background service and then terminates. The second service outlives the first one. I’m not sure whether this is intended behavior or not (maybe a bug report should be filed?) but it’s a workaround (again, a bad one!).


Looks like it’s not possible to get notified when the connection changes:

  • With Android 7.0 CONNECTIVITY_ACTION receivers declared in the manifest won’t receive broadcasts. Additionally receivers declared programmatically only receive the broadcasts if the receiver was registered on the main thread (so using a service won’t work). If you still want to receive updates in the background you can use connectivityManager.registerNetworkCallback
  • With Android 8.0 the same restrictions are in place but in addition you can’t launch services from the background unless it’s a foreground service.

All of this allows these solutions:

  • Start a foreground service and show a notification
    • This most likely bothers users
  • Use JobService and schedule it to run periodically
    • Depending on your setting it takes some time until the service get’s called thus a few seconds could have passed since the connection changes. All in all this delays the action which should happen on connection change

This is not possible:

  • connectivityManager.registerNetworkCallback(NetworkInfo, PendingIntent) cannot be used because the PendingIntent executes it’s action instantly if the condition is met; it’s only called once
    • Trying to start a foreground service this way which goes to the foreground for 1 ms and re-registers the call results in something comparable to infinite recursion

Since I already have a VPNService which runs in foreground mode (and thus shows a notification) I implemented that the Service to check connectivity runs in foreground if the VPNService doesn’t and vice-versa. This always shows a notification, but only one.
All in all I find the 8.0 update extremely unsatisfying, it seems like I either have to use unreliable solutions (repeating JobService) or interrupt my users with a permanent notification. Users should be able to whitelist apps (The fact alone that there are apps which hide the “XX is running in background” notification should say enough). So much for off-topic

Feel free to expand my solution or show me any errors, but these are my findings.

Leave a Comment