Widgets BASICS (medium)

Source: Internet
Author: User
Tags event listener home screen

Reference: Http://developer.android.com/guide/topics/appwidgets/index.html 6. How to Use appwidgetproviderappwidgetprovider to inherit from broadcastreceiver
Widget broadcasts are simply classified and encapsulated with a unified interface for processing for ease of use.

Appwidgetprovider only accepts broadcasts related to app widgets, such as APP widget update, deletion, enabled, and disabled broadcasts. When the broadcast is received, the following functions are called: onUpdate()

This function is called when the system requests to update the App Widget at a frequency defined by updatePeriodMillis in AppWidgetProviderInfo.

If no configuration activity is defined, this function is also called when the user adds the app widget. At this time, initialization can be done, such as setting the event listener of the view to start a temporary service. If configuration activity is defined, you need
When activity is completed, intent is sent to appwidgetprovider to call the function. onDeleted(Context,
int[])
This function is called when the app widget is removed from the app widget host (such as home screen. onEnabled(Context)If you add an app widget to the app widget host (for example, home screen) without an app widget instance in the app widget host, this function is called .. You can do some initial work in this function. If you want to open a database connection or perform other operations only once on multiple app widget instances. onDisabled(Context)If you remove an app widget from the app widget host (such as home screen), it is the only app widget instance in the app widget host. in this function, you can clear onEnabled(Context)For example, clearing a temporary database. onReceive(Context,
Intent)
When receiving any broadcast, the function is called and executed before the preceding functions are called. Generally, you do not need to reload this function. AppWidgetProvider has provided the default implementation. It classifies broadcast and calls the preceding callback functions (onUpdate ). Note: In Android1.5, there is a BUG that the onDeleted () function cannot be called. To solve this BUG, you can refer to Group
Description in post, rewrite OnReceive () methodThe onDeleted () function can be called normally. For the code, see Example 2. 1. example 2.1: @ Overridepublic void onReceive (Context context, Intent intent) {final String action = intent. getAction (); if (AppWidgetManager. ACTION_APPWIDGET_DELETED.equals (action) {final int appWidgetId = extras. getInt (AppWidgetManager. EXTRA_APPWIDGET_ID, AppWidgetManager. INVALID_APPWIDGET_ID); if (appWidgetId! = AppWidgetManager. INVALID_APPWIDGET_ID) {this. onDeleted (context, new int [] {appWidgetId}) ;}} else {super. onReceive (context, intent );}
} OnUpdate () is the most important callback function in AppWidgetProvider. If you do not define a configuration Activity, this function will be called when the App Widget is added to the App Widget Host. If your App Widget needs to interact with the user, you need to set the listener for the user event and process the event. If your App
If a Widget does not need to create a temporary file or database, or some other work needs to be cleaned up, the onUpdate () function may be the only callback function that needs to be reloaded. If you want to enable an Activity by clicking a button in the App Widget, you can refer to the following code: Example 3 public class ExampleAppWidgetProvider extends AppWidgetProvider {
Public void onUpdate (Context context, AppWidgetManager appWidgetManager, int [] appWidgetIds) {final int N = appWidgetIds. length;
// Perform this loop procedure for each App Widget that belongs to this provider for (int I = 0; I <N; I ++) {int appWidgetId = appWidgetIds [I];
// Create an Intent to launch ExampleActivity Intent intent = new Intent (context, ExampleActivity. class); PendingIntent pendingIntent = PendingIntent. getActivity (context, 0, intent, 0 );
// Get the layout for the App Widget and attach an on-click listener // to the button RemoteViews views = new RemoteViews (context. getPackageName (), R. layout. appwidget_provider_layout); views. setOnClickPendingIntent (R. id. button, pendingIntent );
// Tell the AppWidgetManager to perform an update on the current app widget appWidgetManager. updateAppWidget (appWidgetId, views) ;}} in this AppWidgetProvider, we only reload the onUpdate () callback function. In this function, we set PendingIntentAnd pass SetOnClickPendingIntent (int,
PendingIntent)
The PendingIntent is attached to a button of the App Widget. Note that each item of the appWidgetIds array is operated in sequence in a loop. The array includes all the apps created through the AppWidgetProvider.
The id of the Widget instance. By using this method, you can create multiple instances of the App Widget and update them at the same time. However, only the updatePeriodMillis schedule of an App Widget instance manages the updates of all the App widgets. For example, the update schedue of an App Widget is once every two hours. I first added an instance of the App Widget, and then added another instance after an hour, in this case, its update is still handled through the update schedue of the first App Widget, and the second will be ignored (they will update every two hours, instead of adding two updates to schedue every other hour) note: Because AppWidgetProvider is extended from BroadcastReceiver,
Therefore, you cannot ensure that AppWidgetProvider continues to run after the callback function is called. (For more information about the life cycle of BroadcastReceiver, see BroadcastReceiver.) If your App
The initialization of a Widget takes up to several seconds (for example, a WEB request is required). If you want the AppWidgetProvider process to run for a long time, you can enable a Service in onUpdate. In this Service, you can update your App Widget so that you do not have to worry about being forced to close AppWidgetProvider due to ANR errors. For examples of using Service in App widgets, see 《 Widgets basics Appendix 1 (WordWidget. java)For simple use of App widgets, see 《 Widgets basics Appendix 2 (ExampleAppWidgetProvider. java)7. broadcast Intents broadcast that receives App widgets AppWidgetProvider extends from BroadcastReceiver
Widget broadcasts are simply classified and encapsulated with a unified interface for processing for ease of use. You can implement a BroadcastReceiver by yourself and rewrite it onReceive(Context,
Intent)
Method to process the following Intent:

  • ACTION_APPWIDGET_UPDATE
  • ACTION_APPWIDGET_DELETED
  • ACTION_APPWIDGET_ENABLED
  • ACTION_APPWIDGET_DISABLED

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.