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
Related
I want to get latest updates from my server when I turn on the internet and generate a notification, but when the app is closed/killed/swiped from recent items, there is no way to keep my service alive and listening to network change event so that I can ping my server, I'm not sure how to do it and how other apps such as whatsapp does it when we receive new notification the moment we turn on mobile internet.
Any help is appreciated, thanks in advance.
Making clear what #GobuCSG commented. You have the following options:
Run your application all the time via a foreground service and listen for wifi connected broadcast. This is not a good solution and wastes battery life.
Schedule jobs using the WorkManager API.
The WorkManager API was developed specifically for the purpose you specified. The API allows you to set required conditions for your job to run, such as network connectivity. It also persists scheduled jobs through device reboots, so you don't have to. The only downside is that you don't have fine tune control of when the code runs, as one of the goals of WorkManager is to save battery life by delaying and batching jobs.
EDIT:
Another option is to use push notifications. This is useful if your are developing a messaging app and you want your server to push a message to the client so they can be notified of a received message. But, if all you need to do is establish contact with your server once a day, then you should use WorkManager.
I was reading android documentation for register a listner on network change for android pie and below versions.
I found out , for version above lolipop i can run a job service which monitor network changes.
So my question is
If it monitor network changes irrespective of the app is running or not. how do i take care this job service in my app activity life cycle.
curently what i have done is i am scheduling this job in onResume and stopping in OnDestroy in my activity. but i want it run on every network changes
I have an app that connects to the Bitmessage network, i.e. is downloading and processing data from the P2P network all the time. Also, it should (optionally) only use WiFi/unmetered networks.
I've implemented this by using the JobScheduler, but unfortunately it has a timeout of 10 minutes (apparently even 1 minute on Lollipop).
So in short, how do I implement a service that
automatically starts when WiFi is available
automatically disconnects when a metered network is used
doesn't time out
works on all Android versions since Lollipop
As you've found, a JobScheduler is not the correct component for continuously running in the background. The correct component for that is a foreground service.
From your requirements:
automatically starts when WiFi is available
You should still use JobScheduler for this. Your JobScheduler doesn't do any work itself: it just starts your foreground service. You can use Firebase JobDispatcher if you'd like it to work back to API 14 and only need to run on devices with Google Play services.
automatically disconnects when a metered network is used
In your foreground service, you should programmatically register a listener for the CONNECTIVITY_ACTION Broadcast. In the callback, you should check the result of isActiveNetworkMetered() (available on API 16) and, if true, stop your foreground service.
doesn't time out
A foreground service has no time out: it'll continue to run until you stop the service. It is highly recommended that the notification required to make a service a foreground service have an action to allow the user to stop your service manually.
As mentioned, the job scheduler is not intended for tasks that run forever. That is what foreground services are for.
In your case, I'd suggest using a job to monitor for the startup constraints that you want (metered network and so on). When that job runs, you start a foreground service to do the actual work, and return false from onStartJob().
Then, while your foreground service is running, just watch for the loss of metered networking directly using the connectivity & network APIs, and shut down the foreground service when appropriate.
I need to allow user to use my app even if network is not available and make server calls when network is available. I am using alarm manager-broadcast receiver- service pattern. I periodically retry to make api calls. I also added the boot receiver. But when user kills the app using overview screen, the alarms are not triggered again. Is there a better way to implement this common scenario?
You may use:-
1. JobScheduler
2. SyncAdapter
These will help you in case of periodically retry to make API calls
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.