Analysis of appwidget processing by launcher in Android: appwidgethost role

Source: Internet
Author: User
Tags sqlite database

Tian haili @ csdn

2012-8-21

 

Launcher plays the appwidgethost role in the entire android appwidget system. This article analyzes how launcher processes appwidgets, including: processing after appwidgetprovider is selected; processing of appwidget information during launcher initialization (including loading for the first time and normal loading after.

 

As you can see in "select and bind appwidget in Android", launcher initiates the selection operation. appwidgetpickactivity in settings obtains all installed appwidgetproviders for users to choose, return to onactivityresult () of the activity that starts it ().

 

1. Processing after launcher obtains the appwidget

 

First, let's look at the classes defined in launcher to process appwidgets:

 


Figure 1 related classes of appwidget in launcher

 

  • LauncherIs an activity;
  • Inherit the appwidgethostLauncherappwidgethostUsed to operate the appwidgethost function. overrideoncreateview () is used to create your own appwidgethostview-launcherappwidgethostview;
  • LauncherappwidgethostviewUsed to change the behavior habits of click operations;
  • Launcher makes userfolder, livefolder, and appwidget into a certain data model and abstracts them using iteminfo. The corresponding appwidget is expressed using launcherappwidgetinfo.

 

The sequence diagram in Figure 2 describes how the launcher processes the appwidget after the appwidgetpickactivity is returned.


Figure 2. Processing appwidget by launcher after picked

 

Execution Process:

1. In onactivityresult (), you can find in requestcode and resultcode that appwidget is selected successfully. You can get appwidgetid from the returned data: intent; [seq #1]

2. Get info: appwidgetproviderinfo through appwidgetid; [seq #5 ~ #6]

3. Create the launcherappwidgetinfo instance and add it to the data model launchermodel. [seq #7]

4. Create apphostview through launcherappwidgethost. createview (); [seq #8 ~ #15]

  • Oncreateview () in override is executed. Create launcherappwidgethostview in oncreateview (); [seq #8 ~ #10]
  • In appwidgethost. createview (), set appwidgetproviderinfo to appwidgethostview; [seq #11]
  • Appwidgethost. in createview (), obtain the remoteviews provided by appwidgetprovider through appwidgetservice [appwidgethost, appwidgetprovider, and appwidgetservice run in different processes. In this case, remoteviews cannot have content, that is, appwidgetprovider cannot be guaranteed. onupdate () has been executed.] [seq #12 ~ #13]
  • In appwidgethost. createview (), update appwidgethostview with remoteviews; [seq #14]
  • Returns the created appwidgethostview instance. [seq #15]

5. Set the tag-launcherappwidgetinfo instance to appwidgethostview. [Seq #17]

 

Finally, launcherappwidgethostview is added to the current screen to display the corresponding display part. At this time, there may be no content in remoteviews. Here, only a certain amount of space is occupied in the workspace.

 

When appwidgetprovider obtains the updated broadcast and executes onupdate () and onupdate (), it creates remoteviews and uses appwidgetmanager. after updateappwidget () is updated to appwidgetservice, appwidgetservice performs appwidgethost update through the callback of the registered iappwidgethost.


Figure 3. appwidgethost updated

 

Section #3 in remoteviews implementation in Android describes the subsequent processing of remoteviews.

 

 

During the initialization process, launcher loads the appwidget during the first database creation according to the configuration; instead of the first database creation, it loads the appwidget content in the database into the data model.

Ii. When launcher creates a database for the first time, it processes the appwidget

 

Classes related to launcher database operations


Figure 4 launcherprovider

 

  • LauncherLauncherproviderOperations on the database; appwidget related items are inTable_favoritiesForm; launcherprovider.AuthorityDefines the database operation portal and combines launchersettings. favorites.Content_uriThis URI is used for specific operations.
  • It is stored in SQLite, and all the sub-classes of sqliteopenhelper are launcherprovider.DatabasehelperTo operate the SQLite database.
  • Database table_favoritiesFiledInLaunchersettings. favorites.

 

When launcher creates a database for the first time, launcherprovider. databasehelper. oncreate () will be executed and the appwidget will be processed as follows:

 


Figure 5. appwidget processing when launcher creates a database for the first time

 

Execution Process:

1. Remove launcher as content related to appwidgethost; [seq #4]

2. parse the content in default_workspace.xml. If it is related to appwidget:

A) apply for appwidgetid; [seq #8 ~ #9]

B) insert the parsed content into the table_favorites form. [seq #10]

C) bind appwidgetid to appwidgetprovider; [seq #11]

 

In fact, this process condenses a series of processes, such as selecting appwidgetprovider and then binding it. Only the appwidgetprovider to be used here and the position of the screen to be put are determined in the configuration, so it can be automatically completed directly.

 

For example, in RES/XML/default_workspace.xml, configure the appwidget "Power Control:

<appwidget       launcher:packageName="com.android.settings"       launcher:className="com.android.settings.widget.SettingsAppWidgetProvider"       launcher:screen="3"       launcher:x="0"       launcher:y="0"       launcher:spanX="4"       launcher:spanY="1" />

Which attributes of the appwidget in default_workspace.xml are specified by favorite in res/values/attrs. xml:

   <!-- XML attributes used by default_workspace.xml -->   <declare-styleable name="Favorite">       <attr name="className" format="string" />       <attr name="packageName" format="string" />       <attr name="screen" format="string" />       <attr name="x" format="string" />       <attr name="y" format="string" />       <attr name="spanX" format="string" />       <attr name="spanY" format="string" />       <attr name="icon" format="reference" />        <attr name="title" format="reference" />       <attr name="uri" format="string" />   </declare-styleable>

 

3. launcher starts loading the appwidget in the database normally

 

3.1 Data Model in launcher


Figure 6. Brief Data Model in launcher

 

  • LaunchermodelIs a broadcastreceiver;McallbacksThe object to be notified when the model change is recorded;MappwidgetsAppwidget information.
  • LauncherImplement launchermodel.Callbacks, Registered to launchermodel, when the model changes, do the corresponding processing.

 

3.2 launcher Data Model Initialization

 


Figure 7 initialization of the launcher Data Model

 

Execution sequence:

1. When launcher is created, launcher. oncreate () is executed;

2. Get launcherapplication through getapplication (); When launcherapplication is created (launcherapplication. oncreate:

A) instantiate the launchermodel and pass the launcherapplication itself into it;

B) register broadcast for launchermodel;

3. Pass the launcher itself through setlauncher () of the launcherapplication;

Launcherapplication. setlauncher () calls launchermodel's initialize () to pass in the launchermodel. Callbacks instance of launcher;

4. instantiate the appwidgethost of launcherappwidgethost and register the iappwidgethost into appwidgetserivce through startlistening.

 

 

3.3 load and bind a workspace

 

When you need to load the data model, the startloader () of launchermodel will be executed. Launchermodel starts a loadertask thread to execute the load and bind operations.

 


Figure 8. Attach and bind the launchermodel to the Workspace

 

Execute the loading process:

1. Use launchersettings. favorites. content_uri to query all data. [seq #1 ~ #3]

2. Obtain the type of the current record from the launchersettings. favorites. item_type field; [seq #4 ~ #7].

3. For the appwidget type (whose type is launchersettings. favorites. item_type_appwidget), obtain other fields of appwidget attention and assign them to launcherappwidgetinfo; [seq #8 ~ #9]

4. Add the launcherappwidgetinfo instance to mappwidgets; [seq #10]

 

Execute the binding process:

Through the implementation of launchermodel. callbacks, that is, launcher, execute:

  • Startbinding ();
  • Run bindappwidget () on all widgets in mappwidgets ().

 

Run launchermodel. Callbacks. bindappwidget () to execute it in launcher.

 

3.4 attach an appwidget to Launcher

 


Figure 9. launcher bindappwidget

 

This process is the same as that in Figure 2. For more information, see.

 

Summary

This article describes:

  • After selecting an appwidgetprovider, launcher creates a local appwidgethostview through appwidgethost to present the content provided by appwidgetprovider through remoteviews. The corresponding launcherappwidgetinfo is added to the launchermodel data model.
  • Launcher (appwidgethost)/appwidgetservice/appwidgetprovider is running in different processes. The execution order is uncertain, which makes the content of remoteviews time-sensitive. However, as long as remoteviews are updated, appwidgethost will be updated with the notification.
  • When the system first executes (just after the machine is burned or the factory settings are restored), the database is initialized for the first time. The initial appwidget information is loaded from default_workspace.xml and added to the launchermodel data model.
  • During normal boot process (NonAfter the machine is burned or the factory settings are restored), The appwidget information is read from the database and added to the launchermodel data model.

 

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 information described in this article is provided by appwodgetprovider described in this article.

Analysis of appwidget processing by launcher in Android: appwidgethost role

This article

Implementation of remoteviews in Android

How to Implement remoteviews internally and how to use remoteviewsupdate appwidgethostview.

 

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.