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.
- 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.
- 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.
- 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.
- 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
source share