Android N: dealing with lack of CONNECTIVITY_CHANGE - android

My AppWidgetProvider (which is of course a BroadcastReceiver) is registered to receive CONNECTIVITY_CHANGE, so that I can update the widgets as soon as network is restored (though only if required, i.e. if a previous update was missed due to lack of connectivity).
But as set out here, this will no longer be possible in Android N. The suggestion is to use JobScheduler, which allows you to specify that the Job should run only when connected to a network by way of the .setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY) method.
However, I can't see how this can be used to replicate my desired behaviour. It seems to me that the .setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY) method will cause the Job not to run at all if there is no network at the time that the Job is scheduled, and it will not cause the Job to wait patiently until there is a network and then run (which is what I need).
So, how do I retain the behaviour I need, with Android N?

I am using Job Schedulers and it does not depend on whether network is available or not while scheduling the job. It provides certain conditions and when those conditions are satisfied job is scheduled. You can set a repetitive job and also specify minimum time gap between the jobs. For example whenever I connect my phone to charging PlayStore start updating apps as the Job has charging constraint.
If you schedule a job and conditions are not met, it will schedule when network is available. Try to disconnect network and open someone profile on Facebook. It will ask you to load profile on network availability. Now, connect to network and you will get the notification once profile is loaded.

try to add google ping for network change that will help you for
network change and all cause i have same issue when i was developing
with socket services. So may be it will also helps you.

Related

Foreground service vs WorkManager for location tracking

Let's say I want to build an app which requests current location periodically (e.g., every 10 minutes, this number should be configurable) and submits to a server.
I'm aware that Foreground Service and WorkManager are normally suggested for this kind of scenario. However which is would suit more? Below are my thoughts and doubts.
WorkManager - is mainly for deferrable background work whose execution is guaranteed. However I know that from Android 8 (API 26) background location was introduced and that restrict location to be updated only a few times every hour https://developer.android.com/about/versions/oreo/background-location-limits. Thus this perhaps doesn't meet the periodical updates as per the requirement.
ForegroundService - is perfect for something that runs and needs to make users aware of. It's recommended for this kinda scenario (location tracking) for privacy purpose. Google also creates a sample app to promote this practice https://github.com/android/location-samples/tree/master/LocationUpdatesForegroundService.
From the above analysis, it seems ForegroundService is the one. However I also found that WorkManager has a built-in support to use Worker in conjunction with ForegroundService via androidx.work.impl.foreground.SystemForegroundService https://developer.android.com/topic/libraries/architecture/workmanager/advanced/long-running#long-running-kotlin
That makes me confused as to what should I use and what Google really recommend for this specific scenario.
Anyone has any idea?
If you want to communicate somehow with the service then use foreground service and if you want to have some processed input based on something else you did in that work manager then choose work manager.
Work manager doesn't have option to redeliver intents and all other commands like start sticky etc...
Since work manager is more suitable for syncing data with db, processing a file etc..
If you were to ask me, I'd choose foreground service since you can add a type location to the xml tag when you register it in the manifest.
Both of these solutions don't survive OEMs aggressive battery restrictions since WorkManager's work can be deferred and if I want instant execution combined with wake locks I can easily do it in the foreground service since it also has a binder option that works well for UI sync.

How call webservice in every hour if internet connected to device

I want to call web service in the background when internet is connected. If it is connected I want to call in every one hour if internet is turned off then stop the service. How can I? It needs to work for all versions from 16 to 27.
There is broadcast in android which notifies you if network status changed. In there you can register alarm manager for a period of one hour and check if the internet was connected then call the web service.
If broadcast informed you the network was disconnected then you can cancel your alarm manager.
Here is documentation for listening to network status change:
https://developer.android.com/training/monitoring-device-state/connectivity-monitoring
and here is good tutorial about using alarm manager in android
https://en.proft.me/2017/05/7/scheduling-operations-alarmmanager-android/
also google introduced workManager which does this work for you in the very simple way
The WorkManager API makes it easy to specify deferrable, asynchronous
tasks and when they should run. These APIs let you create a task and
hand it off to WorkManager to run immediately or at an appropriate
time. For example, an app might need to download new resources from
the network from time to time. Using these classes, you can set up a
task, choose appropriate circumstances for it to run (like "only while
device is charging and online"), and hand it off to WorkManager to run
when the conditions are met. The task is still guaranteed to run, even
if your app is force-quit or the device is rebooted.
here is link to documentation :
https://developer.android.com/topic/libraries/architecture/workmanager

Intentservice in JobScheduler's context

In our app, we have periodic sync tasks. So we have implemented Job scheduler to fire every sync interval. When the job gets fired, we are starting an intent service and this job gets killed. Intent service starts the 3rd party sync from internet/Server
Questions:
Is this design ok for Oreo and Nougat?
If the intent service takes few mins to complete, will the system allow?
Ref: Medium link
If your job inent service depend on network restriciton you need to set setRequiredNetworkType as there are some new changes done in oreo developement for job scheduler you shold opt for that part as per your flow please take in to account that
Calling setRequiredNetworkType defines network as a strict requirement for your job. If the network requested is not available your job will never run. See setOverrideDeadline(long) to change this behavior. Calling this method will override any requirements previously defined by setRequiredNetwork(NetworkRequest); you typically only want to call one of these methods.
When your job executes in onStartJob(JobParameters), be sure to use the specific network returned by getNetwork(), otherwise you'll use the default network which may not meet this constraint.
for more details go to this source of the above answer https://developer.android.com/reference/android/app/job/JobInfo.Builder.html#setRequiredNetwork(android.net.NetworkRequest)

Difference between periodic and recurring job in JobScheduler

What is the difference between recurring and periodic job in Firebase JobScheduler? I can't find any detailed documentation about recurring flag in its API.
A little background: Since Android Oreo restricted broadcast receivers including the one for changed network state, I am looking for a way to report information based on currently connected wifi network. I don't want to waste user's battery by setting up too frequent periodic job but I need that information in reaction to connected wifi relatively quickly, in a matter of units of minutes.. I was hoping that recurring job would fire off when set up constraints changed, but I can't find any confirmation for this, or any other information regarding recurring jobs for that matter

JobScheduler run job at most once every time the conditions are met

NOTE: THIS IS NOT A DUPLICATE, PLEASE READ THE QUESTION
I want to run a job every time the device is Charging & ON WIFI. This job has to run at most once every time these conditions are met.
This means that if I leave the phone charging overnight with wifi ON the job should not repeat itself.
Only when I unplug and replug the job is allowed to execute again. Same goes for when I turn wifi off and on.
builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_UNMETERED)
.setRequiresCharging(true)
//.setPeriodic(TimeUnit.SECONDS.toMillis(10))
.setPersisted(true)
Job scheduler provides methods like setPeriodic but that will run my job every X amount of time. Not really what I want.
The job is not critical, I don't need it to be executed right away after the conditions are met, and I'm also OK with it not executing at all sometimes (meaning it's ok for it not to be run when conditions are met for a short period of time)
Is it possible to achieve this using job scheduler? The documentation on this is pretty scare.
If you're not using setPeriodic, then your job would only run once when your other constraints are set. However, your requirements mean you need to schedule a new job when you leave those conditions - JobScheduler does not offer that API, nor does Android offer any API that does that that also works with Android 8.0's Background Execution Limits (with the exception of continuously running a foreground service).

Categories

Resources