I have found the following parts of code in a widget:
<appwidget-provider>
android:updatePeriodMillis="0"
<appwidget-provider/>
Now, my question is now that: what it does mean with updatePeriodMillis set to 0.
I know that it is set for the how often the widget will receive remoteview or others. so, what is the significance to set it to 0.
When you Set updatePeriodMillis to 0, You are actually disabling the update period on that particular widget. Means, that property is no more applicable to that widget.
According to android documentation:
The updatePeriodMillis attribute defines how often the App Widget framework should request an update from the AppWidgetProvider by calling the onUpdate() callback method. The actual update is not guaranteed to occur exactly on time with this value and we suggest updating as infrequently as possible—perhaps no more than once an hour to conserve the battery. You might also allow the user to adjust the frequency in a configuration—some people might want a stock ticker to update every 15 minutes, or maybe only four times a day.
Note: If the device is asleep when it is time for an update (as defined by updatePeriodMillis), then the device will wake up in order to perform the update. If you don't update more than once per hour, this probably won't cause significant problems for the battery life. If, however, you need to update more frequently and/or you do not need to update while the device is asleep, then you can instead perform updates based on an alarm that will not wake the device. To do so, set an alarm with an Intent that your AppWidgetProvider receives, using the AlarmManager. Set the alarm type to either ELAPSED_REALTIME or RTC, which will only deliver the alarm when the device is awake. Then set updatePeriodMillis to zero ("0").
According to Android developers
How often, in milliseconds, that this AppWidget wants to be updated. The AppWidget manager may place a limit on how often a AppWidget is updated.
This field corresponds to the android:updatePeriodMillis attribute in the AppWidget meta-data file.
Note: Updates requested with updatePeriodMillis will not be delivered more than once every 30 minutes.
Related
I'm trying to make an app that does certain tasks according to a certain battery level (say a notification when battery level reaches 10%).
The problem is when i launch it the app just checks the event once instead of continuous monitoring.
e.g. (say i launched the app when battery was 11%. Since the notification is scheduled at 10% it wont give any notification. It just check the event a single time instead of continuously monitoring i.e. when battery drains to 10% while running the app,nothing happens)
What is the possible solution to make the code run continuously ?
Try using the Alarm Manager Class: developer.android.com/training/scheduling/alarms.html.
If you pass in your battery check as the pending intent, the trigger time which is when you first want to check (In your case probably the interval time after the current time) and then an interval time (Too often can slow things down a lot, so try to find a time that suits your application, e.g. every few minutes.)
This should then fire on each interval which you could use to trigger your battery check and further functionality.
I have an Android widget scheduled to update every hour
android:updatePeriodMillis="3600000"
However even when I change the system clock (forward one hour or more) the widget update method is not being called, no visual changes or logs happen.
I even wait a couple of minutes thinking that the OS may wait till the next minute because it doesn't need precision, but still nothing triggers.
Changing the system time won't trigger the basic widget updates?
You are doing right, but there is no guarantee it will be updated at exact time you expected. It could be delayed. Check the documentation.
The actual update is not guaranteed to occur exactly on time with this value.
The widget uses another internal clock to update itself, You'll need to wait 30 minutes in order to see the change and this cannot be hasted by changing the device's date.
If less time is required, then AlarmManager is the option.
IMPORTANT: If widget frequency is 30 minutes, ALL USERS will update the widget exactly at 00:00 and 00:30 of each hour. So if the widget consults a service the server will get peaks of requests every half an hour.
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.
How to check the current device time in background at regular intervals(say every 1 hour) even after the app is closed using startService() method?Thanks
You don't necessarily need a service, depending on what you want to do with the time you get.
As a general rule, to do something at regular intervals you can use AlarmManager.setInexactRepeating() (or setRepeeating() if you really need a perfect periodicity). Use the PendingIntent of this method to start whatever (BroadcastReceiver, Activity, Service...) when the alarm is triggered.
This question got me to think of the way I was writing my battery widget.
ACTION_BATTERY_CHANGED firing like crazy
I'm concerned with the power usage. I saw how frequent ACTION_BATTERY_CHANGED gets fired. Even if you check if the battery level actually changed before you perform the update operation, it still looks a little expensive.
So, which of either listening to ACTION_BATTERY_CHANGED in service or setting alarmservice for like every 5min - 10min to update the widget sound more efficient?
The widget would just be switching ImageView source according to battery level and updating a TextView. Though might consider drawing the text into the image using Bitmap.
You should not care about ACTION_BATTERY_CHANGED events, just use Alarm to check the battery status every 5-10 minutes, that is pretty much enough for your purpose.
On the other hand, do you seriously think someone needs another battery widget in 2013 ?
That depends entirely on if the battery events are raised more frequently than the alarm.
If you're making an app widget make sure you're not updating when not visible.
I made a live wallpaper based on battery level and see no adverse affects of using the battery changed events