Detecting connection changes on Android 7.0 Nougat when an application is in the foreground

Nougat has changed the way it handles CONNECTIVITY_CHANGED intentions (basically ignoring it, forcing developers to use the task scheduler), so I have to wonder:

If I have an application that is in the middle of getting some data (and I checked if the phone was turned on at the time I made the request, but the user is on the move and the phone is connecting to another Wi-Fi access point, for example) and it doesn’t work, how can I detect that the connection has been restored and I am stopping the attempt of fetxhing data?

So, in this case, my application is in the foreground, I think that Chrome (for android) has a similar function.

Do I need to interview him myself? Or is there some kind of event that is allowed at this time?

+9
source share
5 answers

Applications that run can still listen on CONNECTIVITY_CHANGE in their main thread if they request a notification using BroadcastReceiver.

https://developer.android.com/about/versions/nougat/android-7.0-changes.html

+4
source

Although you can use Andromeda’s answer, this decision is not Google’s intention. Your question was what to do if the connection is lost, and you need to resume work when the network service returns.

Although CONNECTIVITY_CHANGE works technically, for this specific purpose it has always been a bit of a hack and it will stop working in Nougat as soon as your application goes into the background. What you really should use is the Task Scheduler API. Google has offered us many options with different requirements and features.

  1. Jobcheduler

JobScheduler has been added to Lollipop and adds a scheduler that can wait for a network connection to schedule a job. It may even depend on the type of connection, checking for unmeasured or non-roaming connections. This option is not backward compatible, but works without Google Play Services.

  1. Gcm network manager

GcmNetworkManager is a direct port of JobScheduler functionality for versions prior to Lollipop, but it requires Google Play services. GcmNetworkManager is no longer supported by the Firebase Job Dispatcher.

  1. Firebase Task Manager

Firebase JobDispatcher provides another job scheduling tool for versions prior to Lollipop, which uses Google Play Services by default, but can be configured to not require this dependency.

  1. Workmanager

I just edited this post adding that Google has replaced all three of the previous job schedulers with WorkManager, which is better than the others in almost every way. You can set the required type of network for your work. You can even link tasks together one after another.

All of these options will satisfy your battery needs, and your tasks will continue to be scheduled, even if the device briefly wakes up from standby.

Here is more information on the various options with examples provided by Google:

https://developer.android.com/topic/performance/scheduling.html https://developer.android.com/topic/performance/background-optimization.html#sched-jobs

+10
source

According to doc :

Applications targeting Android 7.0 (API level 24) and higher do not receive CONNECTIVITY_ACTION broadcasts if they declare their broadcast receiver in the manifest. Applications will still receive CONNECTIVITY_ACTION broadcasts if they register their BroadcastReceiver with Context.registerReceiver() , and this context is still valid.

+5
source

In my case, I signed up for a broadcast with the CONNECTIVITY_CHANGE filter in the service and it works.

How to keep Service alive is another story :)

+2
source
 public class ConnectivityReceiver { public static boolean getWifiStatus(Context context) { // To get System Connectivity status ConnectivityManager cm = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo activeNetwork = cm.getActiveNetworkInfo(); if (null != activeNetwork) { // Check For Wifi Status if(activeNetwork.getType() == ConnectivityManager.TYPE_WIFI) return true; else return false; } return false; } public class NetworkMgr extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { ConnectivityReceiver cf = new ConnectivityReceiver(); boolean status = cf.getWifiStatus(context); if(status) { Toast.makeText(context,"Wifi Connection is On.", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(context,"Wifi Connection is Off.", Toast.LENGTH_SHORT).show(); } } } 
0
source

All Articles