Android widget Package Description and application, androidwidget

Source: Internet
Author: User

Android widget Package Description and application, androidwidget

A widget package stores custom components.

Develop custom components;

In fact, the App Widgets section in the Android API Development Guide has been clearly stated. The following is just a summary of your understanding.

--
AppWidget is a widget displayed on HomeScreen and provides intuitive interactive operations. In the HomeScreen dialog box, press and hold the Widget to create the Widget. In the displayed dialog box, hold the Widget and drag it to the recycle bin to delete it. You can create Multiple widgets at the same time.

The implementation of AppWidget mainly involves the following categories:
AppWidgetProvider
RemoteViews
AppWidgetManager

1.
First, you must provide an XML file (located in res/layout/...) that defines the Widget interface layout /..), note that the components used must be supported by RemoteViews. Currently, native APIs support the following components:
FrameLayout
LinearLayout
RelativeLayout

AnalogClock
Button
Chronmeter
ImageButton
ImageView
ProgressBar
TextView

* If other components are used, the android. view. InflateExceptionn exception occurs when the Widget is created.

PS: as a result, some functions or styles cannot be implemented. For example, basic list or text editing boxes cannot be implemented directly. To customize the View in a Widget, you can only modify the framework to provide support for the corresponding components.

2.
Then, you need to provide an xml file to define the basic properties of the Widget and place it in the res/xml/... directory.
If Eclipse is used, perform the following operations:
1) Create an xml/directory under the res/directory
2) create an xml file (with any name) and set the type to AppWidgetProvider.
3) set parameters on the pop-up Interface

The main parameters are as follows:
MinWidth: defines the width of the Wdiget component.
MinHeight: defines the height of the Wdiget component.
UpdatePeriodMillis: Update time period
InitialLayout: Widget layout File
Configure: If you need to enable an Activity before it is started, the complete Class Name of the Activity is provided here (the implementation of the Activity is slightly different from that of the general Activity)

* Number of cells calculated for the Widget size * 74)-2. The API says it is to prevent integer rounding during pixel computing, So-2... not very clear

A complete example:
Add Xml code to favorites

<? Xml version = "1.0" encoding = "UTF-8"?>
<Appwidget-provider xmlns: android = "http://schemas.android.com/apk/res/android"
Android: minWidth = "80dp"
Android: minHeight = "32dp"
Android: updatePeriodMillis = "86400000"
Android: initialLayout = "@ layout/widget_provider"
Android: configure = "com. demo. widget. MyWidgetConfiguration">
</Appwidget-provider>



3.
After the xml is defined, create a subclass inherited from AppWidgetProvider. AppWidgetProvider is actually a BroadcastReceiver, which provides the following functions:
OnReceive (Context, Intent)
OnUpdate (Context, AppWidgetManager, int [] appWidgetIds)
OnEnabled (Context)
OnDeleted (Context, int [] appWidgetIds)
OnDisabled (Context)
You can rewrite the preceding functions to listen for Widget status changes and handle them accordingly.

The specific call conditions of the above functions are summarized as follows:
[Start-No confiure Activity]
OnReceive
OnEnabled -- the first widget is displayed.
OnReceive
OnUpdate -- refresh the page

[Start-Activity with confiuration]
OnReceive
OnUpdate

[Drag]
<Stateless change>

[Periodic update]
OnReceive
OnUpdate

[Delete]
OnReceive
OnDeleted -- the widget is deleted.
OnReceive
OnDisabled -- the last widget is removed.

[Insufficient position at startup]
OnReceive
OnEnabled
OnReceive
OnUpdate
OnReceive
OnDeleted
OnReceive
OnDisabled

* OnReceive is triggered when the status changes. Generally, this function does not need to be rewritten.

After a brief understanding of AppWidgetProvider, let's look at the specific implementation.
Here we create a MyAppWidgetProvider that inherits AppWidgetProvider:
Java code collection code

Public class MyWidgetProvider extends AppWidgetProvider {

Static final String TAG = "widget ";

/**
* Update
*/
Public void onUpdate (Context context, AppWidgetManager appWidgetManager, int [] appWidgetIds ){
Log. I (TAG, "onUpdate ");
}

/**
* Triggered when the first Widget component is started.
*/
Public void onEnabled (Context context ){
Log. I (TAG, "onEnabled ");
}

/**
* Triggered when the last Widget is disabled.
*/
Public void onDisabled (Context context ){
Log. I (TAG, "onDisabled ");
}

/**
* Triggered when any Widget component is deleted
*/
Public void onDeleted (Context context, int [] appWidgetIds ){
Log. I (TAG, "onDeleted ");
}

/**
* The above function will be triggered first before being triggered. Generally, this function does not need to be rewritten.
*/
Public void onReceive (Context context, Intent intent ){
Log. I (TAG, "onReceive ");
Super. onReceive (context, intent );
}

}



OnUpdate, as its name implies, updates Widgets. The update cycle defined above acts on this function.
Unlike an Activity, a Widget must be updated by using RemoteViews and AppWidgetMananger. The specific implementation is as follows:
Java code collection code

Public void onUpdate (Context context, AppWidgetMananger appWidgetManager, int [] appWidgetIds ){
Int N = appWidgetIds. length; // Multiple widgets may be started. appWidgetIds records the IDs of these widgets.
For (int I = 0; I <N; I ++ ){
RemoteViews views = new RemoteViews (getPackageName (), R. layout. widget_views );
AppWidgetManager. updateAppWidget (appWidgetIds [I], views );
}
}


It should be noted that, although the RemoteViews parameters are the same, it is best to create a new one for each Widget and then pass it. Otherwise, some errors may occur. For details, refer to AppWidget RemoteViews memory overflow.

Other functions can be implemented as needed.

The elements in the interface created by RemoteViews cannot be obtained. Operations on components in widgets can only be performed by using the limited functions provided by RemoteViews. Common Operations include:
SetOnClickPendingIntent (int viewId, PendingIntent pendingIntent)
SetProgressBar (int viewId, int max, int progress, boolean indeterminate)
SetTextViewText (int viewId, CharSequence text)
SetViewVisibility (int viewId, int visibility)
For details about the Function list, refer to the RemoteViews class in the API.

4.
Finally, update AndroidManifest. xml.
AppWidgetProvider corresponds to a receiver attribute:
Add Xml code to favorites

<Cycler android: name = "MyWidgetProvider">
<Intent-filter>
<Action android: name = "android. appwidget. action. APPWIDGET_UPDATE"> </action>
</Intent-filter>
<Meta-data android: resource = "@ xml/widget_property" android: name = "android. appwidget. provider"> </meta-data>
</Cycler>



5.
Configuration Activity
Configuration Activity is an Activity that is started before the Widget starts. You can set the properties of the Widget.

In res/xml /... add the configure field to the corresponding "Property file" to specify the Activity to be started, and add the configure field to AndroidManifest. in xml, the action provided by this Activity is android. appwidget. action. intenFilter of APPWIDGET_CONFIGURE.

Note that,
If the Configure attribute is set, you must perform the following operations in the specified Activity:
1. Add setResult (RESULT_CANCLE) before setContentView () function in onCreate. If the BACK button is pressed before the Activity Initialization is complete, the Widget will not start;
2. After the setContentView () function (not necessarily in onCreate, before the Activity exits), add the following settings to specify the Widget to be started:
Java code collection code

Int mAppWidgetId = extras. getInt (AppWidgetManager. EXTRA_APPWIDGET_ID, AppWidgetManager. INVALID_APPWIDGET_ID );
Intent resultValue = new Intent ();
ResultValue. putExtra (AppWidgetManager. EXTRA_APPWIDGET_ID, mAppWidgetId );
SetResult (RESULT_ OK, resultValue );

Otherwise, the Widget does not start after exiting the Activity.

> Widget creation steps:
1. Define Widget layout XML-> res/layout /...
2. Define the Widget property file (xml)-> res/xml /...
3. create an AppWidgetProider subclass to implement functions such as onUpdate (). register the handler in manifest and add an action as android. appwidget. action. APPWIDGET_UPDATE IntentFilter, and add the following <meta-data> identifier:
Add Xml code to favorites

<Meta-data android: resource = "@ xml/<property file xml>" android: name = "android. appwidget. provider"> </meta-data>



4. Create a Coniguration Activity (note that setResult is handled properly), add the Configure attribute to the property file, register the activity in manifest, and add an android. appwidget. action. APPWIDGET_CONFIGURE IntentFilter as the action.

A complete example is attached,
The implementation idea is as follows:
1. A Configuration Activity is provided. Only one line of text is displayed here;
2. After exiting, the Widget starts;
3. click the button in the Widget to start an Activity.

(

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.