title: Android O - Detect connectivity change in background
body: <p>First off: I know that <code>ConnectivityManager.CONNECTIVITY_ACTION</code> has been deprecated and I know how to use <code>connectivityManager.registerNetworkCallback</code>. Furthermore if read about <code>JobScheduler</code>, but I am not entirely sure whether I got it right.</p>
  
  <p>My problem is that I want to execute some code when the phone is connected / disconnected to/from a network. This should happen when the app is in the background as well. Starting with Android O I would have to show a notification if I want to run a service in the background what I want to avoid.<br> I tried getting info on when the phone connects / disconnects using the <code>JobScheduler/JobService</code> APIs, but it only gets executed the time I schedule it. For me it seems like I can't run code when an event like this happens. Is there any way to achieve this? Do I maybe just have to adjust my code a bit?</p>
  
  <p>My JobService:</p>
  
  <pre><code>@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
  public class ConnectivityBackgroundServiceAPI21 extends JobService {
  
      @Override
      public boolean onStartJob(JobParameters jobParameters) {
          LogFactory.writeMessage(this, LOG_TAG, "Job was started");
          ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
          NetworkInfo activeNetwork = connectivityManager.getActiveNetworkInfo();
          if (activeNetwork == null) {
              LogFactory.writeMessage(this, LOG_TAG, "No active network.");
          }else{
              // Here is some logic consuming whether the device is connected to a network (and to which type)
          }
          LogFactory.writeMessage(this, LOG_TAG, "Job is done. ");
          return false;
      }
  }
  @Override
  public boolean onStopJob(JobParameters jobParameters) {
      LogFactory.writeMessage(this, LOG_TAG, "Job was stopped");
      return true;
  }
  </code></pre>
  
  <p>I start the service like so:</p>
  
  <pre><code>JobScheduler jobScheduler = (JobScheduler)context.getSystemService(Context.JOB_SCHEDULER_SERVICE);
  ComponentName service = new ComponentName(context, ConnectivityBackgroundServiceAPI21.class);
  JobInfo.Builder builder = new JobInfo.Builder(1, service).setPersisted(true)
      .setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY).setRequiresCharging(false);
  jobScheduler.schedule(builder.build());
              jobScheduler.schedule(builder.build()); //It runs when I call this - but doesn't re-run if the network changes
  </code></pre>
  
  <p>Manifest (Abstract):</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"&gt;
  
      &lt;uses-permission android:name="android.permission.INTERNET" /&gt;
      &lt;uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /&gt;
      &lt;uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /&gt;
      &lt;uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" /&gt;
      &lt;uses-permission android:name="android.permission.VIBRATE" /&gt;
  
      &lt;application&gt;
          &lt;service
              android:name=".services.ConnectivityBackgroundServiceAPI21"
              android:exported="true"
              android:permission="android.permission.BIND_JOB_SERVICE" /&gt;
      &lt;/application&gt;
  &lt;/manifest&gt;
  </code></pre>
  
  <p>I guess there has to be an easy solution to this but I am not able to find it.</p>