0

I have found a code about checking whether or not the device is connected to the internet. The code is as follows

private boolean isNetworkAvailable() {
    ConnectivityManager connectivityManager 
          = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
    return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}

My question is what if this returns TRUE but the device suddenly lost its connection? How can I get a signal that will let me know I have lost my internet connection at ANY point while my app is running?

ttulka
  • 6,803
  • 3
  • 31
  • 43
el diablo
  • 35
  • 6

1 Answers1

0

You can achieve in-app targeting below N.

In AndroidManifest.xml you have to add the following permission.

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

Here is your reciver in manfest file.

<receiver android:name=".NetworkStateReceiver" >
    <intent-filter>
        <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
    </intent-filter>
</receiver>

Here is you broadcast receiver

public class NetworkStateReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

    if(isDeviceOnline()){

      }    
}


private boolean isDeviceOnline() {
        ConnectivityManager connMgr =
                (ConnectivityManager) Collect.getInstance().getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
        return networkInfo != null && networkInfo.isConnected();
    }

But Declaring a broadcast receiver for android.net.conn.CONNECTIVITY_CHANGE is deprecated for apps targeting N and higher.

In general, apps should not rely on this broadcast and instead use JobScheduler or GCMNetworkManager.

This issue flags code that either * negatively affects battery life, or * uses APIs that have recently changed behaviour to prevent background tasks from consuming memory and battery excessively.
Generally, you should be using JobScheduler or GcmNetworkManager instead.
For more details on how to update your code,

More info

Schedule network jobs on unmetered connections

When using the JobInfo.Builder class to build your JobInfo object, apply the setRequiredNetworkType() method and pass JobInfo.NETWORK_TYPE_UNMETERED as a job parameter. The following code sample schedules service to run when the device connects to an unmetered network and is charging:

public static final int MY_BACKGROUND_JOB = 0;
...
public static void scheduleJob(Context context) {
  JobScheduler js =
      (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE);
  JobInfo job = new JobInfo.Builder(
    MY_BACKGROUND_JOB,
    new ComponentName(context, MyJobService.class))
      .setRequiredNetworkType(JobInfo.NETWORK_TYPE_UNMETERED)
      .setRequiresCharging(true)
      .build();
  js.schedule(job);
}

Find more detail here

Amkhan
  • 168
  • 1
  • 1
  • 10