[Android] desktop Widget Dynamic Refresh

Source: Internet
Author: User

There is such a configuration for Dynamic Refresh of desktop widgets,

There is an attribute under res/xml <appwiet-provider>

Android: updatePeriodMillis = "86400000"

Official notes:

[Plain]
The updatePeriodMillis attribute defines how often the App Widget framework shocould 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.
Chinese Translation:

The updatePeriodMillis attribute specifies how often the AppWiget framework sends a request to update by calling the onUpdate () method of AppwidgetProvider. The actual update does not guarantee that the value is updated on time, we recommend that you update the battery at least once every hour to save the battery power. The configuration on your page allows users to adjust the update frequency, just as some people want the stock market tracker to update every 15 minutes, or update every four times a day.
In addition, a note is provided:
[Plain]
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 es, 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 ").
Chinese Translation:
If the screen is sleep, the screen will be awakened when updatePeriodMillis is used to update the widget. If you do not update the battery every hour, you may have a serious impact on the battery standby time. However, if you need frequent update operations, but you may not need to wake up the screen when updating the screen, you can perform update operations based on alarm instead of waking up the device. In this way, use AlarmManager to set an alert clock with intent so that your AppwidgetProvider can accept the update operation. Set the alarm type to ELAPSED_REALTIME or RTC, so that the alarm intent is passed only when the device is awake. Set updatePeriodMillis = 0.


Some problems may occur in actual operations,

1. When the SDK is above 1.6, when the value of updatePeriodMillis is less than half a hour, it will become invalid.

2. Some problems may occur during service update or alarm update.


Next, let's talk about my personal understanding. If something is wrong, please submit it.

First, let's talk about the appwidget creation process.

1. A list will appear on the long-pressed desktop. This list should be the list of applications that have registered AppWidgetProvider in manifest.

[Html]
<Cycler android: name = "com. test. TestAppWidgetProvider">
<Intent-filter>
<Action android: name = "android. appwidget. action. APPWIDGET_UPDATE"/>
</Intent-filter>
<Meta-data
Android: name = "android. appwidget. provider"
Android: resource = "@ xml/test_appwidget_info"/>
</Cycler>

2. When you click the icon, the android. appwidget. action. APPWIDGET_UPDATE intent is triggered,
The onReceive () method of AppWidgetProvider is executed.

Then the onUpdate () method is executed, and an id is assigned to the appWidget.

Then, the AppWidgetConfigure Activity is called and you can configure the widget size.

[Html]
<Activity android: name = ". test. TestAppWidgetConfigure">
<Intent-filter>
<Action android: name = "android. appwidget. action. APPWIDGET_CONFIGURE"/>
</Intent-filter>
</Activity>

Then there is the refresh method. I have tried several methods to talk about it.
1. I first used handler to refresh the AppWidgetProvider onUpate method and open a thread to send messages once every 10 seconds.

This method has several problems: 1> problems are found during application upgrade. 2> the thread will be suspended.

2. Use the service to send appWidgetManager and appWidgetIds to the service and refresh the service. The result is invalid. I don't know why.

3. Send android. appwidget. action. APPWIDGET_UPDATE to refresh the broadcast. It is recommended to use service or alarm for better distribution than alarm. The following describes the process of using alarm.

This method also encountered a problem at the beginning. I always wanted to refresh it in the onUpdate method, but the fact was wrong. 1> Create an alarm in onUpdate and send intent once every 10 seconds. Then, an alarm sending intent will be created every time the onUpate is refreshed. This is a problem.

2> During refresh, I will retrieve the appwidget id and appWidgetManager. updateAppWidget (appWidgetIds [0], views); refresh, the result is an abnormal situation where my own widget switches back and forth with a widget rendered on the desktop of the system. I am not clear about the situation.


Finally, I am using the following methods:

Set alarm in onUpdate

[Java]
AlarmManager alarmMgr = (AlarmManager) context. getSystemService (Context. ALARM_SERVICE );
Intent intent = new Intent (AppWidgetManager. ACTION_APPWIDGET_UPDATE );
Int requestCode = 0;
PendingIntent pendIntent = PendingIntent. getBroadcast (context, requestCode, intent, PendingIntent. FLAG_UPDATE_CURRENT );
// Send the broadcast in 5 seconds, and then repeat the broadcast every 10 seconds. Broadcasts are directly sent to AlarmReceiver.
Long triggerAtTime = SystemClock. elapsedRealtime () + 10*1000;
Int interval = 10*1000;
AlarmMgr. setRepeating (AlarmManager. RTC, triggerAtTime, interval, pendIntent );

In onReceive, refresh AppWidgetId from AppWidgetConfigure and set mAppWidgetId to static.
[Java]
Intent intent = getIntent ();
Bundle extras = intent. getExtras ();
If (extras! = Null ){
MAppWidgetId = extras. getInt (
AppWidgetManager. EXTRA_APPWIDGET_ID,
AppWidgetManager. INVALID_APPWIDGET_ID );
}

[Java]
AppWidgetManager. getInstance (context). updateAppWidget (TestAppWidgetConfigure. mAppWidgetId, views );

Key:
Take a look at the onReceive method in the TestAppWidgetProvider parent class. intent does not need to include parameters when alarm is set in onUpdate, so that only the onReceive method will be called each time alarm refreshes and the onUpate method will not be called.

[Java]
/**
* Implements {@ link BroadcastReceiver # onReceive} to dispatch callto the various
* Other methods on AppWidgetProvider.
*
* @ Param context The Context in which the specified er is running.
* @ Param intent The Intent being already ed.
*/
// BEGIN_INCLUDE (onReceive)
Public void onReceive (Context context, Intent intent ){
// Protect against rogue update broadcasts (not really a security issue,
// Just filter bad broacasts out so subclasses are less likely to crash ).
String action = intent. getAction ();
If (AppWidgetManager. ACTION_APPWIDGET_UPDATE.equals (action )){
Bundle extras = intent. getExtras ();
If (extras! = Null ){
Int [] appWidgetIds = extras. getIntArray (AppWidgetManager. EXTRA_APPWIDGET_IDS );
If (appWidgetIds! = Null & appWidgetIds. length> 0 ){
This. onUpdate (context, AppWidgetManager. getInstance (context), appWidgetIds );
}
}
}
Else if (AppWidgetManager. ACTION_APPWIDGET_DELETED.equals (action )){
Bundle extras = intent. getExtras ();
If (extras! = Null & extras. containsKey (AppWidgetManager. EXTRA_APPWIDGET_ID )){
Final int appWidgetId = extras. getInt (AppWidgetManager. EXTRA_APPWIDGET_ID );
This. onDeleted (context, new int [] {appWidgetId });
} Www.2cto.com
}
Else if (AppWidgetManager. ACTION_APPWIDGET_ENABLED.equals (action )){
This. onEnabled (context );
}
Else if (AppWidgetManager. ACTION_APPWIDGET_DISABLED.equals (action )){
This. onDisabled (context );
}
}


Author: iamcai723

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.