Tian haili @ csdn
2012-8-22
This article analyzes the internal implementation of remoteviews in Android based on the appwidget application scenario.
I learned from the analysis and application of appwidget in Android: appwidgetprovider and the analysis of appwidget processing by launcher in Android: appwidgethost role, in Android, the graphic resources of appwidget are provided by appwidgetprovider through remoteviews, while the display is that appwidgethost displays the content provided by remoteview on the local view through appwidgethostview. Appwidgetprovider and appwidgethostview run in different programs. The graphic elements they communicate with and the bridge between click feedback is remoteviews.
For convenience and consistency, appwidgetprovoder, the content provider of remoteviews, is called the remote end. appwidgethost, the content provider of remoteviews, is called the local end.
1. Provide content to remoteviews -- settingsappwidgetprovider
Is settingsappwidgetprovider (located in COM. android. settings. in the widget package) as appwidgetprovider to get the update notification, create remoteviews, and put the remote terminal's response intent and graphic elements into the Order chart in remoteviews.
Figure 1. Content and listener for remoteviews
In the figure,
1. When setting creates remoteviews, packagename and layoutid are passed in and saved.Packagename is very importantBecause the layoutid and various other resources are relative to this program. resources can be obtained only by obtaining the corresponding context through packagename, otherwise, other programs cannot obtain [seq #1] of these resources.
2. Settings: Set the pendingintent of the response to remoteviews after the view indicated by viewid in layoutid is clicked. [seq #2 ~ #5].
- Remoteviews creates setonclickpendingintent and passes in the ID and intent. setonclickpendingintent saves these values;
- Setonclickpendingintent is a subclass of remoteviews. Action. Use addaction () to add setonclickpendingintent to mactions: arraylist <remoteviews. Action> and save it.
3. Settings: Set the imagesourceid of the view indicated by viewid in layoutid to remoteviews [seq #6 ~ #10].
- Remoteviews has many setxyz () methods, which are used to set values based on different types;
- Setxyz () creates a reflectionaction and passes in viewid and value, as well as "setimageresource" as methodname. reflexyz action saves these values;
- Reflectionaction is a subclass of remoteviews. Action. You can use addaction () to add reflectionaction to mactions: arraylist <remoteviews. Action> and save it.
This section describes a sub-process. In the future, appwidgetmanager will be used to put the created remoteviews into the appwidget system, so that the appwidgethost side of the appwidget will update and display the content carried by remoteviews.
The Content Setting Process on the remote end only sets these parameters, while remoteviews only saves these parameters with different remoteviews. Actions. The internal structure is described below.
Note: The parameters here are all on the remote end and are valid in remotecontext.
Ii. Internal Structure of remoteviews
Is a class graph related to remoteviews.
Figure 2. remoteviews class diagram
RemoteviewsTo save the mpackage and mlayoutid of the remote end, and use mactions: arraylist <remoteviews. Action> to save various actions.
MpackageAndMlayoutidIt is the [seq #1] in the above figure that is passed in when constructing remoteviews;
MactionsWhen setting various remote terminal response intent and graphical elements, save them to the corresponding action and add the action here for saving;
MlayoutidVarious controls in the remote end are set through settextviewtext (), setimageviewresource (), setprogressbar (), and other functions. These methods call the setType() [Type can be boolean/byte/short/INT/long/Char/string/uri/bitmap/bundle, etc] and saved to reflectionaction.
SetonclickpendingintentIs used to send a pendingintent notification when a local user clicks the viewid. Save the viewid and pendingintent in the setonclickpendingintent constructor.
ReflectionactionIt is used to obtain remote resources through the reflect Mechanism during local display. Save viewid, methodname, type, and value in the reflectionaction constructor.
ViewgroupactionAndSetdrawableparametersIt is also a subclass of remoteviews. Action, which is not used in this scenario and has similar basic principles. You can analyze it yourself.
3. Display remoteviews content-appwidgethostview
After the content is provided for remoteviews in Figure 1, appwidgethost will be notified to update the remote end through iappwidgethost. updateappwidget (), and the content provided by remoteviews will be displayed on appwidgethostview on the local end. The following sequence diagram describes the process.
Figure 3. Content in remoteviews is displayed locally
Figure:
1. Obtain the packagename and layoutid of the remote end (appwidgetprovider) in remoteviews, and create the remote context -- remotecontext through packagename. [Seq #1 ~ 6]
2. Use the apply () method of remoteviews to really start the action of listening for the click operation. Use the remote layout to obtain the locally used view. [Seq #7 ~ 20]
2.1. clone a local layoutinflater; [seq #8]
2.2. Use the cloned layoutinflater to execute inflate on the remote layoutid to obtain the hierarchy of the view described by layout, that is, the rootview used later. [seq #9 ~ 10]
2.3. Execute merge mapply on The View obtained in 2.2. [Seq #11 ~ 19]
Performapply () performs the apply () operation on all actions in mactions. In this way,
2.3.1 for setonclickpendingintent, [seq #12 ~ 15]
- Use findviewbyid (viewid) of the rootview (the total view of layout on the remote end obtained in 2.2) to find the view to listen to; [seq #13]
- Set the listener for the view to be listened on. [Seq #14]
2.3.2 for reflectionaction, [seq #16 ~ 19]
- Use findviewbyid (viewid) of the rootview (the total view of layout on the remote end obtained in 2.2) to find the object view to be set; [seq #17]
- Then, through the reflect mechanism, execute the methods in the view implementation class (for example, setimageresource () and set the corresponding resource ID to it. [seq #18]
3. Add the obtained View to the local view system. [Seq #21]
The following is a reflectionaction. Apply () code snippet for setting content through the reflect mechanism (ignore error handling and non-critical parts ):
@ Override public void apply (view root) {final view = root. findviewbyid (viewid); Class Param = getparametertype (); // use this. type to get class: Int. class class Klass = view. getclass (); // This class is defined in remote layout. Here it is imageview method = Klass. getmethod (this. methodname, Param); // methodname is the method name in the View class: setimageresource (INT) Try {// execute imageview. setimageresource (value), value is resid method. invoke (view, this. value) ;}catch (exception ex) {Throw new actionexception (Ex );}}
Iv. Summary
Content provider of remoteviews, which provides displayed resources and intent for listening to click events;
The local display party of remoteviews obtains the display resources in the view through remoteviews and adds them to the local graphics system to display the remote resources locally.
Articles for further reference
Through this series of other articles, you can obtain the information associated with this article:
Android appwidget framework
Appwidget system framework.
Select and bind an appwidget in Android
Launcher initiates the selection process. This document describes the selection and binding process. You can refer to this article to see the complete process of selecting, binding, and adding a display system.
Appwidget Analysis and Application in Android: appwidgetprovider
The appwidgetprovider described in this article creates remoteviews and sets imageviewresource and onclickpendingintent.
Analysis of appwidget processing by launcher in Android: appwidgethost role
The remoteviews provided by appwidgetprovider described in this article, the time to start the real application of remoteviews in launcher.
Implementation of remoteviews in Android
This article.