Q:

Check internet connectivity on android nougat in background thread

I am developing an android application targeting android nougat. It should do a remote sql to local database sync, if connected to internet, in background thread. I tried using broadcast receiver to detect connectivity, but it is not firing on android N. How can i do that (Check internet availability) in background? An example would be helpful
I tried this
Android manifest
 <receiver android:name=".NetworkChangeReceiver">
        <intent-filter>
            <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
        </intent-filter>
    </receiver>

NetworkChangeReciever.java
public class NetworkChangeReceiver extends BroadcastReceiver {
@Override
public void onReceive(final Context context, final Intent intent) {
        Toast.makeText(context,"Internet state changed",Toast.LENGTH_LONG).show();
}

A:

I got an answer to my question.
Broadcast receiver with action 
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />, which is registered in manifest will not fire in android N. But one can register a broadcast programatically and make it work. But it will not work when the app is killed.
Then in my case, I had two options (thats what I thought), Schedule the sync operation with 

GCM Network manager 
OR
Job scheduler

The problem with job scheduler is that it works only above API level 21, but it doesn't need Google play service to work. On the other hand GCM Network manager had backwards compatibility, but needs google play service to run. 
Then I visited Intelligent Job Scheduling, and they said, go with Firebase Job dispatcher,

Firebase JobDispatcher supports the use of Google Play services as an
  implementation for dispatching (running) jobs, but the library also
  allows you to define and use other implementations: For example, you
  might decide to use JobScheduler or write your own, custom code.
  Because of this versatility, we recommend that you use this Firebase
  JobDispatcher if your app targets a version of Android lower than 5.0
  (API level 21).

And I took that advice,
and this is what I did,
Created a service (SyncService.java) which does the sync job,
added that service to manifest
<service
    android:exported="false"
    android:name=".SyncService">
        <intent-filter>
            <action android:name="com.firebase.jobdispatcher.ACTION_EXECUTE"/>
        </intent-filter>
</service>

And in my activity, created and executed a Job to sync
Job syncJob=jobDispatcher.newJobBuilder()
            .setService(SyncService.class)
            .setRecurring(true)
            .setLifetime(Lifetime.FOREVER)
            .setRecurring(true)
            .setLifetime(Lifetime.FOREVER)
            .setTrigger(Trigger.executionWindow(3,600))
            .setConstraints(Constraint.ON_ANY_NETWORK) //does job when network is available
            .setReplaceCurrent(true)
            .setTag("syncService")
            .build();
jobDispatcher.mustSchedule(syncJob);

And this works in background.

