Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect connectivity changes on Android 7.0 Nougat when app is in foreground

Nougat changed the way it handles CONNECTIVITY_CHANGED intents (basically ignoring it, forcing devs to use the job scheduler) so this leaves me wondering:

If I have an app that is in the middle of retrieving some data (and I checked if the phone was online at the time I did the request but the user is on the move and the phone connects to a different wifi access point, for example) and it fails, how do I detect that the connection has been restored and I cam retry fetxhing the data?

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

Do I need to poll it myself? Or is there some event that is allowed at that time?

like image 965
REJH Avatar asked Aug 29 '16 15:08

REJH


People also ask

How to fix common Android Nougat issues?

This is probably the most common issue faced by many of the Android 7.0 Nougat users. Here are the solutions. The primary things to do are: reduce the display brightness, screen lock time, turn off the Wi-Fi or Bluetooth connection while not in use and close all the unwanted apps running in the background.

What is Android 7 0 Nougat update?

Android 7. 0 nougat has not reached all devices yet but if you have recently purchased a Nexus or Google Pixel device, you must be enjoying the new Android OS which has brought various exciting and useful features to help you get the best of your smartphone. This update is available since August and already the update version of it Android 7.

Can I use Wi-Fi instead of cellular data on Android 7 Nougat?

This is a very annoying Android 7.0 Nougat issue. While you can use the Wi-Fi instead of cellular data, there are times when no option is left other than data and it requires the problem to be fixed as soon as possible. Before you contact your carrier, here are some easy workarounds to try which can resolve the issue in no time.

What's new in Android 7 for work?

Android 7.0 contains changes for apps that target Android for Work, including changes to certificate installation, password resetting, secondary user management, and access to device identifiers. If you are building apps for Android for Work environments, you should review these changes and modify your app accordingly.


2 Answers

According to doc:

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

like image 163
amrezzd Avatar answered Oct 28 '22 13:10

amrezzd


While Andromeda's answer could be used, that solution is not Google's intended choice. Your question was what to do when a connection is lost and you need to resume operation when network service returns.

While CONNECTIVITY_CHANGE technically works, it was always a bit of a hack for this specific need and it will stop working in Nougat as soon as your app goes in the background. What you should really use is a job scheduler API. Google has offered us many options with differing requirements and features.

  1. JobScheduler

JobScheduler was added in Lollipop and adds a scheduler which can wait for network connectivity to schedule a job. It can even depend on the kind of connection, checking for unmetered or non-roaming connections. This option has no backwards compatibility, but works without Google Play Services.

  1. GCM Network Manager

GcmNetworkManager is a direct port of the JobScheduler functionality to versions before Lollipop, but it requires Google Play Services. GcmNetworkManager has been mostly deprecated by Firebase Job Dispatcher.

  1. Firebase Job Dispatcher

Firebase JobDispatcher provides another means to schedule jobs for versions before Lollipop, which uses Google Play Services by default, but can be configured to not require this dependency.

  1. WorkManager

I just edited this post to add that Google replaced all of the three previous job schedulers by WorkManager, which is better than the others in almost every way. You can set a required network type for your job to run. You can even chain jobs together one after another.

All of these options will satisfy your need in a battery-friendly manner and your jobs will still get scheduled even if the device wakes up briefly from Doze mode.

Here is more information about 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

like image 21
colintheshots Avatar answered Oct 28 '22 12:10

colintheshots