Take the Gmail app as an example. Whether the phone is on or not, it polls every 10 minutes or so to download new emails which may have arrived since you last checked.
I know how to create a new service and bind to it. But I can see a few ways to accomplish this:
Bind once, and have the service run in an infinite loop, sleeping for 10 minutes between each loop
Bind and unbind right when it's done, scheduling the next bind somehow in 10 minutes
Using the AlarmManager class to schedule future polls
What are the trade offs? How does the Gmail app accomplish it?
Thanks!
Gmail app uses pushing, not polling. I suggest using this technique instead, polling is a battery killer in mobile devices.
To implement pushing, take a look at C2DM.
If you still want to poll, the recommended way would be to set up a periodic alarm in the AlarmManager.
UPDATE: Google has deprecated C2DM and replaced it with Google Cloud Messaging (GCM)
UPDATE: Google has deprecated GCM and replaced it with
Firebase Cloud Messaging (FCM)
For a continuous, but not intensive poll like the one you comment (in the range of minutes between polls), I would implement it with AlarmManager. That way you make sure the phone wakes up to poll without the need for a wakelock, which would destroy your battery. As CommonsWare pointed out, you will still need to implement a wakelock for the time your code is executing, but you can release it as soon as the code is done, avoiding keeping the phone on while just waiting. See his comment for an example on how to implement it.
I would use a Service if, instead, you need faster polls during a shorter period of time (seconds between each poll), since setting alarms does not make sense to such short periods, and the battery would drain anyway.
Related
I am developing a flutter application, however I would like a service to be able to run constantly without stopping in order to make an api request every 15 minutes and then send a notification to the user (Android /IOS). I would also like the service to start automatically with the smartphone. I've been stuck on this for more than a week now and I've been browsing the forums looking for a solution but I can't find what I'm looking for. Thank you in advance for any help
You don't do it like that on Android. You cannot count on an application not being killed in the background. Instead, you use JobScheduler or WorkManager to set an alarm and wake you up every so often to perform whatever job you need. These methods can also ensure you're scheduled at startup of the phone.
Also, 15 minutes may or may not happen- Doze mode may cause your app to be delayed and make requests less frequently than that if the phone goes to sleep (although 15 minutes is fairly safe, plus or minus a few).
I have a business requirement that the Android app needs to report to the server every few seconds (all the time, 24/7) on a dedicated device.
First I thought that it can be done with the PeriodicWorkRequest, but I've read its minimum interval is 15mins.
What would be the best way to achieve this? What mechanism I can rely on to be sure that the process won't be killed?
Is it possible to do with WorkManager? Should I have a foreground service with a loop?
I think you could schedule it the other way. Make your server send a notification to your android device,could be done easily via firebase. Then handle notification in your app and report back to the server.
I am making an application which contains performing network calls to look for updates. Can anyone help with some best practices to perform this task. I can make a service to run on background and perform network operation using Handler at regular intervals but this would consume a lot of data and battery. Is there any other way to do this?
Use an AlarmManager or JobScheduler (depending on API level), and pick a sane frequency. Doze mode will stop you from going too insane.
You could work with Push Notifications from any server when the backend recognizes an update. It's better for the server itself to listen to updates at a regular interval of time rather than the app, because of the reasons you listed.
When the app receives a push notification, it would mean that it needs updating.
There are a couple of options for you:
Azure Notifications Hub: https://learn.microsoft.com/en-us/azure/notification-hubs/
Firebase Cloud Messaging: https://firebase.google.com/docs/cloud-messaging/
i was thinking if there is anyway to sync android app with a web/website account. Basically, if the user logs in through the web, i want the android app to log in/start up/show notification. Should i use a background service in android to monitor the account or something else
Please help
Thanks
As far as I can see, you have a couple of options:
Use a background service (as you suggested) which is constantly running and checking. This will be battery consuming.
Use the AlarmManager to schedule a repeating alarm to check on a scheduled interval. This will be less battery consuming but also less immediate (unless you set a low interval which will consume a lot of battery).
Use a push message. This will be the most accurate and least battery consuming for your phone, but it's also the most advanced (difficult) to set up.
Based on your requirement, optimized solution is to use GCM - Push Notification. Based on the whatever action occurs at web, you just need to send a push notification to clients. Based on the push notification IDs, client needs to perform appropriate action.
I am working on an app that periodically polls server for new data (I know I should use GCM, but its not an option here). I am using AlarmManager.setInexactRepeating() to atleast allow system to batch networking request to save some battery.
However now I am working on a update and I do not want to poll at night. How would I do this?
The usual approach would be have one-time alarms that each would schedule the next one appropriately to avoid that night-time interval.
But this approach, as far as I see, doesnt allow that batching that inexactRepeating does ..
Is there a way to set one-time inexact alarm?
Thanks