I have two types of homescreen app widgets 1. Consisting of text views and 2. Consisting of listview. The first displays data from the arrays and the second one pulls information from the server (i.e. involves networking operations). And they both need to be updated every minute. Now for each case which of the following techniques are best?
a) Handler
b) Alarm Manager
c) JobScheduler
d) Firebase JobDispatcher
e) SyncAdapter
f) ..anything else if there is...
As a beginner to android it is difficult to decide which technique is the best for the battery life of the devices. Thanks for the help in advance.
As a beginner to android it is difficult to decide which technique is the best for the battery life of the devices
The answer is "none of the above — do not attempt to update app widgets once per minute". Google is going to great lengths to prevent you from doing this sort of thing, because users got tired of app developers wasting battery life.
Handler
Handler
has little to do with what you are trying to solve. You can use Handler
and postDelayed()
to implement an in-process timing loop, but you could also use classic Java solutions like ScheduledExecutorService
.
However:
This requires you to have a process running all of the time. As of Android O, this in turn will require you to have a foreground service, meaning that you will have to be showing a Notification
. Users get irritated by this, as well as by apps that seem to be consuming system RAM continuously for no good reason. And, in practice, you cannot have a process running all of the time anyway, though with a sticky foreground service, you can come close.
This will not update the app widgets when the device is in Doze mode on Android 6.0+.
This will not update the app widgets when the device is asleep.
Alarm Manager
AlarmManager
would allow you to not necessarily have a running process all of the time. However, this will not update the app widgets when the device is in Doze mode on Android 6.0+.
JobScheduler
JobScheduler
jobs are inexact (i.e., you do not control exactly when they are run), and I do not know if they can be invoked as frequently as once per minute. Also, this will not update the app widgets when the device is in Doze mode on Android 6.0+, and it will not update the app widgets when the device is in partial Doze mode on Android 7.0+.
Firebase JobDispatcher
This requires you to have a server, to manage registrations and send high-priority FCM messages every minute. My guess is that Google eventually will prevent servers from doing this.
SyncAdapter
You have no control over exactly when syncs occur. Also, this will not update the app widgets when the device is in Doze mode on Android 6.0+, and it will not update the app widgets when the device is in partial Doze mode on Android 7.0+.