Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which value is of higher precedence, updatePeriodMillis or the value associated with the Alarm?

I understand that the parameter updatePeriodMillis determines how often an app widget gets updated according to the specification in the widgetproviderinfo.xml present in /res/xml

<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
    android:configure="com.example.appwidget.ConfigurationActivity"
    android:initialLayout="@layout/layout_appwidget_large"
    android:minHeight="115dp"
    android:minWidth="250dp"
    android:updatePeriodMillis="1800000" ><!-- 30mins -->
</appwidget-provider>

This approach has a drawback in that it updates the widget by waking up the phone at the designated interval if the phone is sleeping. So the issue is about battery consumption which is a major issue if the interval is very small.

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"). to The code would look something like this:

final Intent intent = new Intent(context, UpdateService.class);
final PendingIntent pending = PendingIntent.getService(context, 0, intent, 0);
final AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarm.cancel(pending);
long interval = 1000*60;
alarm.setRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime(),interval, pending);

So my question is as follows: Let's say the AlarmManager is used to carry out the update. Additionally, if in the widgetproviderinfo.xml, updatePeriodMillis is NOT set to 0, then Which value will take the precedence? The value specified as part of the Alarm or updatePeriodMillis?

like image 246
user3575020 Avatar asked Jun 27 '14 02:06

user3575020


2 Answers

Both of them will trigger an onUpdate() call.

If you set an alarm at 20 minutes and your updatePeriodMillis to 30 minutes.

Then u place your widget on the home screen. After 20 minutes, the alarm will trigger an onUpdate to be called (can be any other function of your choosing actually). Then 10 minutes later (in total, 30 mins since widget placed on screen), updatePeriodMillis will trigger an onUpdate(). Then 10 minutes later (40mins since widget placed on screen), the alarm will trigger another onUpdate(). And finally 20 minutes later (60mins since widget placed on screen) updatePeriodMillis will trigger another onUpdate().

like image 77
Tikiboy Avatar answered Nov 09 '22 05:11

Tikiboy


From android DOC: Note: Updates requested with updatePeriodMillis will not be delivered more than once every 30 minutes. [enter link description here][1] http://developer.android.com/reference/android/appwidget/AppWidgetProviderInfo.html#updatePeriodMillis

so basically if you set updatePeriodMillis to less than 1800000, android wouldn't call your onUpdate method until 30min passeed

like image 29
moh.sukhni Avatar answered Nov 09 '22 06:11

moh.sukhni