I have a widget that I need to update frequently with new content only while the device is awake.
I will use an alarm manager for this and set alarm type to either ELAPSED_REALTIME or RTC, as suggested on "App Widgets" guidelines on android developer site.
The problem is that I need to update the widget every 5 seconds (probably configurable) while the screen is on.
Wherever I searched people say that 5 seconds is insane, but I haven't yet really understand if this is gonna be a problem if I update only when the device is awake. Is there a different approach I can take to this problem? How clock widgets do this?
While experimenting I realized that when the screen goes off the alarm still triggers.
It stops triggering only when the device goes into deep sleep, in which case 5 seconds are too few anyway for the device to have time to go into sleep.
So what I did is filter the SCREEN_ON broadcast and schedule the alarm every 5 seconds. Then filter SCREEN_OFF broadcast and cancel the alarm.
Related
I have two questions.
I want fire a Broadcast receiver using AlarmManager and show a notification in onReceive method. Should I use from WakeLoke for this?
What is different between setAlarmClock() and setExactAndAllowWhileIdle() ?
I use (as you wrote) the onReceive method to start a newWakeLock and it works fine for me.
The difference lies in the behavior in doze mode (Doze Mode: https://developer.android.com/training/monitoring-device-state/doze-standby).
I do not know your exact problem, but I worked very hard to develop an app which contains few timers and every timer should make a notification at the exact time even the screen is locked and the device is in the doze mode. My solution is to fire an Broadcast over an AlarmManager with the setExact(...) method.
Answer your question in reverse order
.2. setExactWhileIdle guarantees that if the system is not sleeping and not in doze, the alarm will go off within 1 minute of the given time. if the system is in doze mode, the alarm will go off within 15 minutes of the given time. In practice, if the system is not in doze mode or low on battery, the alarm will go off on time. On the other hand, setAlarmClock is the closest one can get to a guarentee that the system will deliver the alarm at a specific time; this does come at a relatively large drain on battery. So, if your goal is to implement highly time sensitive notifications such as an alarm clock, then use setAlarmClock. Otherwise try to avoid it.
.1. according to the documentation, upon an alarm being dispatched from setExactAndAllowWhildIdle or setAlarmClock:
the app will also be added to the system's temporary power exemption list for approximately 10 seconds to allow that application to acquire further wake locks in which to complete its work.
My suggestion is that if all you are doing is posting a notification, then a wake lock is not necessary. Otherwise, if you are doing longer running work, use a wake lock
Obligatory Disclaimer: battery drain is a real thing. please don't make an app that drains the battery. do everything in your power to design your app not to disturb the systems power optimization. All exact alarms and especially setAlarmClock disrupt the systems attempts to optimize battery. If its necessary, then its necessary. Otherwise, do not do it.
I have developed an App Widget that requires to be updated every 10 minutes. In Android documentation about App Widgets it is specified that if the widget needs to be updated more frequently than once per hour, it is recommended to use AlarmManager and set the alarm type to either RTC or ELAPSED_REALTIME so the alarm is only delivered when the device is awake.
I have implemented the AlarmManager and my widget is updating correctly every 10 minutes. After several testings, using both RTC and ELAPSED_REALTIME alarm types, I have seen that my widget is still getting updated after the device goes to sleep. I can see in LogCat that my widget is getting updated even after 30 minutes that the device went to sleep (30 minutes after the screen turns off).
My question is, if the alarm is still been delivered and my widget is updated every 10 minutes even after the device has gone to sleep how is using AlarmManager with RTC or ELAPSED_REALTIME as the documentation says more battery efficient than just setting 600,000 milliseconds for updatePeriodMillis property on my widget provider xml file?
A couple of things:
Both RTC and ELAPSE_REAL_TIME have WAKEUP and non WAKEUP versions, so if you do not want you widget updating when the phone is sleeping, you could chose the non WAKEUP version. If you set updatePeriodMillis, it is going to wake the device either way.
When you set your alarm using setInexactRepeating() instead of setRepeating(), Android will bundle multiple inexact alarms and fire them at the same time, which is more battery efficient. Also according to the doc:
As of API 19, all repeating alarms are inexact.
Another thing is that, according to the AppWidgetProviderInfo doc:
Updates requested with updatePeriodMillis will not be delivered more
than once every 30 minutes.
so it probably wouldn't be useful to you if you needed to update every 10 minutes.
I'm have a widget that is being updated from a service. When the user then turn off the screen, the service stops and won't start to update the widget again when the user wake up the device. So on to my question, is there some way to "restart" the service when the user wake up the device? I know I can keep the service keep going when the screen is locked, but I think it would drain to much battery then... I have google'd alot but haven't found anything...
Thanks in advance!
EDIT: Okey, this is strange! Now somehow it does that automatically, so it's working now! Thanks anyway!
EDIT 2: Or, now I don't really think it's the screen lock that stops the service, cause it just sometimes stops randomly without crashdialog when I use the phone... Anyone know how I should do to fix that?
For a widget it usually doesn't make sense to keep a service running all the time, you usually update the widget periodically.
The easiest way to do this and to be battery efficient is to schedule an intent using AlarmManager and setAlarmInexactRepeating:
Schedule a repeating alarm that has inexact trigger time requirements; for example, an alarm that repeats every hour, but not necessarily at the top of every hour. These alarms are more power-efficient than the strict recurrences supplied by setRepeating(int, long, long, PendingIntent), since the system can adjust alarms' phase to cause them to fire simultaneously, avoiding waking the device from sleep more than necessary.
The intent should trigger a BroadcastReceiver which in turn should trigger your service to update the widget. Using IntentService is usually a good choice for that.
If you need more control over the intent scheduling, you can also set the alarms yourself in when the service is finished with updating the widget, and supply RTC or ELAPSED_REALTIME as the type of alarms. Both types won't wake up the device if it's sleeping. If the device is not sleeping (but locked) you'll effectively piggy back on other events that woke the device.
If you really need to update as soon as the device is unlocked, the ACTION_USER_PRESENT broadcast might be what you need, but I'd still recommend to implement that as an addition to scheduling a periodic update.
Yes this is Possible!
When the user turn off the screen You Have to handle BroadCast Receiver (http://developer.android.com/reference/android/content/BroadcastReceiver.html)
That handle to Stop service & the service stops and stop to update the widget,
when the user wake up the device you have to Receive Broadcast start to update the widget.
I think it would be The best idea to save more battery.
For More information Please Refer this Link
I'm using alarm manager in my service to set non-waking alarms every 15 seconds to execute a certain task. I don't want to wake the phone up as the task isn't time critical, so i'm using the ELAPSED_REALTIME flag to set the alarm. Here's the code:
alarm.setRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime(), 15 * 1000, intentRecurringChecks);
What I'm noticing in my logs is that the task is getting executed every 15 seconds. Does this mean the phone is staying awake even though it's screen has been turned off for half an hour? Is there a way I can be sure my application's not the one waking up the phone?
I've search about this topic but I can't find a proper answer.
Thanks for your help.
First, you shouldn't use AlarmManager for such timeouts. This is explicitly mentioned in documentation (read the bold part). It's better to use Handler based timers in your case. Here is an example: Repeat a task with a time delay?.
Second, when device is connected via USB, its not going to deep sleep mode. You should disconnect your device wait a minute or two. Attach it back and analyze the logs.
I'm writing a simple Widget for Android which displays information which changes for every day. So the widget needs to be refreshed on midnight. The whole refreshing is implemented as a service and runs nicely, the problem is the invocation:
The only solution I found is to use the AlarmManager to a add an exact reoccurring timer on midnight each day. Then aquire a partial Wake-Lock, to make sure the device stays awake and run the code. This should work as expected but due to the usage of the wake lock, I am waking the device, so I am searching for a slimmer version:
There is no need to wake the device up on exact midnight, it is enough if I receive a timer event the first time the Device is up again on a new day. If the device is sleeping, nobody can look on the widget, so it is ok if the widget updates whenever the device is switched on again.
In other words: How do I run a service on the first moment of a day when the device is not sleeping, thus preventing a wakeup? I still need the device to stay awake than for period of time.
How can this bis done?
You can tell the AlarmManager to delay your invocation until the device wakes up anyway.
Then don't use a WakeLock in the.