Select and bind an AppWidget in Android

Source: Internet
Author: User

When Android adds a widget, AppWidgetHost selects AppWidgetProvider and binds it. Generally, Launcher plays the AppWidgetHost role. AppWidgetProvider can be selected from the list of all installed appwidgetproviders. This article describes the process of selecting and binding AppWidgetProvider.

 

1. Launch a Selection Application

 

The process of selecting a widget on the desktop is actually Launcher. To apply for an AppWIdget selection, you only need to use startActivityForResult () and AppWidgetManager. ACTION_APPWIDGET_PICK is the Intent Action to start another Activity and wait for the selected result.

 


Figure 1 LauncherPick AppWidget class diagram

 

Launcher is an Activity that has the ability and corresponding methods to start another Activity and wait for the result. LauncherAppWidgetHost is also defined in LauncherAppWidgetHost.

 

The sequence of PickAppWIdget initiated by Launcher is as follows:

 


Figure 2. Order of LauncherPick AppWidget

 


Use AppWidgetHost to apply for an AppWidget ID;
Create an AppWidgetManager. ACTION_APPWIDGET_PICK Intent and use the appWidgetId applied above as the parameter of AppWidgetManager. EXTRA_APPWIDGET_ID. Start another Activity to select the AppWidget and wait for the result to be processed.

 

After an Activity that responds to AppWidgetManager. ACTION_APPWIDGET_PICK is sent, the Android system selects the Activity containing AppWidgetManager. ACTION_APPWIDGET_PICK from the installed apk and starts it.

The AppWidgetPickActivity in Settings is such an Activity. Let's take a look at how it works.

 

Ii. static structure of AppWidgetPickActivity

 

The following figure shows the AppWidgetPickActivity class:

 

Figure 3. AppWidgetPickActivity class diagram

 


AppWidgetPickActivity class inheritance: AppWidgetPickActivity-> ActivityPicker-> AlertActivity. ActivityPicker contains PickerAdapter, which is used to PickItem and fill in the data displayed to the user in PickerAdapter. Item.
AppWidgetPickActivity obtains all installed appwidgetproviders through AppWidgetManager. getInstalledProviders (). The information of these appwidgetproviders is stored as AppWidgetProviderInfo.
ActivityPicker, the parent class of AppWidgetPickActivity, implements DialogInterface. OnClickListener and DialogInterface. OnCancelListener to respond to user selection. After you select one of the appwidgetproviders, bind the AppWidgetId to it.

 

3. Obtain all installed AppWidgetProvider

 

Figure 4 shows how to obtain information about all installed AppWidgetProvider and generate PickerAdapter. Item.

 


Figure 4. Obtain the order of the list of all installed appwidgetproviders

 


When AppWidgetPickActivity is created and started, the AppWidgetId parameter is passed in through AppWidgetManager. EXTRA_APPWIDGET_ID, Which is parsed and saved here. [Seq #1 ~ #3]
ActivityPicker, the parent class of AppWidgetPickActivity, calls getItems () in TemplatePattern to obtain the list items. AppWidgetPickActivity implements getItems (): [Seq #4 ~ #9]
Use AppWidgetManager. getInstalledProviders () to obtain all installed appwidgetproviders. The information of these appwidgetproviders is returned in the form of AppWidgetProviderInfo; [Seq #5 ~ #6]
Create PickerAdapter. Item, set corresponding items in AppWidgetProviderInfo to PickerAdapter. Item, and put all these items into items: ArrayList <PickerAdapter. Item>; [Seq #7 ~ #8]
Return from getItems () and return the return value items [Seq #9]

After getItems () brings back the Item and returns it, ActivityPicker will display these items in the form of a list. Next, if you select an item, you can select AppWidgetProvider and bind it with AppWidgetId.

 

4. Select and bind an AppWidgetProvider

 

Figure 5 shows the process of binding an AppWidgetProvider to the AppWidgetId.

 

Figure 5. Sequence of AppWidgetProvider selection and binding

 


The user selected an AppWidgetProvider in the list; [Seq #1]
Get the Intent through the position which in the list. Actually, the packageName and className of the Provider are set to the Component of the Intent; [Seq #2 ~ #5]
Bind the AppWidgetId obtained in Figure 4 #3 (that is, the ID applied by the Launcher AppWidgetHost through AppWidgetHost in Figure 2 #1 & 2) to the AppWidgetProvider. [Seq #6 ~ #7]
Set the selected result through Activity. setResult () and return the appWidgetId as an Intent parameter. [Seq #8 ~ #10]

In this way, the Activity that starts AppWidgetPickActivity, that is, Launcher, gets the selected result in its onActivityResult.

 

Because, after obtaining the results, Launcher will do the work as AppWidgetHost, which will be elaborated in the analysis and application of AppWidget in Android: AppWidgetHost.

 

V. Summary


Launcher initiates the action of selecting AppWidgetProvider.
AppWidgetPickActivity obtains the list of all installed appwidgetproviders and presents them to users. After the user selects this option, bind the AppWidgetId to the AppWidgetProvider.
As AppWidgetHost, Launcher is responsible for displaying the graphic elements passed through RemoteViews in AppWidgetProvider, and notifying users of Operation events to AppWidgetProvider.

 

Further references

Android AppWidget framework

AppWidget system framework.

Select and bind an AppWidget in Android

This article.

AppWidget Analysis and Application in Android: AppWidgetProvider

You can see what information AppWidgetManager/AppWidgetService collects for AppWidgetProvider, which is put in the list of installed AppWidgetProvider. You can also learn how to create and set RemoteViews for AppWidgetProvider.

Analysis and Application of AppWidget in Android: AppWidgetHost

You can see how to create and display the graphic elements provided by AppWidgetProvider in RemoteViews after selecting and binding AppWidgetProvider and Launcher as AppWidgetHost.

Implementation of RemoteViews in Android

The internal implementation of RemoteViews in Android.

 

Related Article

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.