Android Widget package Description and application

Source: Internet
Author: User

A widget pack is a custom component that is stored

Development of custom components;

In fact, the app widgets chapter in the Android API Development Guide has made it clear that the following is just a comb for your own understanding.

--
The Appwidget is the widget shown on the homescreen, providing intuitive interactive operation. By long pressing in homescreen, select the widget widget in the popup dialog box to create it, press and hold the part and drag it to the trash to delete it. You can create more than one widget part at a time.

The implementation of Appwidget mainly involves the following classes:
Appwidgetprovider
Remoteviews
Appwidgetmanager

1.
First, you need to provide an XML file that defines the layout of the Widget interface (located in res/layout/.), it should be noted that the components used must be supported by Remoteviews, and the components currently supported in the native API are as follows:
Framelayout
LinearLayout
Relativelayout

AnalogClock
Button
Chronmeter
ImageButton
ImageView
ProgressBar
TextView

* If other components are used, the widget will cause a Android.view.InflateExceptionn exception when it is created.

PS: This results in some features or styles that cannot be implemented, such as a basic list or text edit box that cannot be implemented directly. If you want to customize the view in the widget, you can only provide support for the corresponding component by modifying the framework.

2.
Then you need to provide an XML file to define the basic properties of the widget, and place it in res/xml/. Directory.
If you are using eclipse, you can do the following:
1) Create the xml/directory under the res/directory
2) Create XML file (name optional), select Type Appwidgetprovider
3) Set the parameters in the convenient interface of the Popup

The parameters for the main settings are as follows:
MinWidth: Defines the width of the Wdiget component
MinHeight: Defining the height of the Wdiget component
Updateperiodmillis: The time period of the update
Initiallayout:widget's Layout file
Configure: If you need to start an activity to set up before you start, give the full class name of the activity here (it will be said later that there is a slight difference from the implementation of the general activity)

*widget size of the calculated cell number *74) -2,api said to prevent the pixel calculation when the integer rounding caused the error so-2 ... I don't understand.

A complete example:
XML code Collection Code

<?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 all defined, the next step is to create a subclass that inherits from Appwidgetprovider, and 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 listen for changes to widget status and handle it accordingly by overriding the above function.

The specific invocation of the above function is summarized as follows:
[Start-No confiure Activity]
OnReceive
onenabled--The first widget is displayed
OnReceive
onupdate--Refresh Interface

[Start-with confiuration Activity]
OnReceive
OnUpdate

Drag
< non-state change >

[Cycle UPDATE]
OnReceive
OnUpdate

Delete
OnReceive
Ondeleted--widget is removed
OnReceive
ondisabled--the last widget was removed

[Insufficient position at start-up]
OnReceive
Onenabled
OnReceive
OnUpdate
OnReceive
ondeleted
OnReceive
Ondisabled

* Each change in state will trigger OnReceive, which is generally not required for the function to be rewritten.

After a brief look at Appwidgetprovider, let's look at a concrete implementation.
Here you create a Myappwidgetprovider inheritance 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 starts
*/
public void onenabled (context context) {
LOG.I (TAG, "onenabled");
}

/**
* Triggered when the last widget component is closed
*/
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 function is triggered before the above function is triggered, and generally does not need to be rewritten
*/
public void OnReceive (context context, Intent Intent) {
LOG.I (TAG, "onreceive");
Super.onreceive (context, intent);
}

}



Where OnUpdate, as the name implies, updates the widget, and the previously defined update cycle is the function.
Widget updates are different from activity and must be aided by 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 widget,appwidgetids may have been started to record 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 is important to note that although the remoteviews parameters are the same, it is best to create a new one for each widget and then pass it, otherwise it will cause some errors. Refer to Appwidget remoteviews memory overflow for details.

Other functions can be implemented as needed.

Because the elements in the interface created by Remoteviews cannot be obtained, the operation of the components in the widget can only be done through the limited functions provided by remoteviews, which are commonly used:
setonclickpendingintent (int viewId, pendingintent pendingintent)
Setprogressbar (int viewId, int max, int progress, Boolean indeterminate)
Settextviewtext (int viewId, charsequence text)
setviewvisibility (int viewId, int visibility)
A list of detailed functions can refer to the Remoteviews class in the API.

4.
Finally, update androidmanifest.xml.
Appwidgetprovider corresponds to a receiver property:
XML code Collection Code

<receiver 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>
</receiver>



5.
Providing configuration Activity
Configuration activity is an activity that starts before the widget is launched, allowing the user to set the widget's properties.

In the res/xml/... Add the Configure field in the corresponding properties file to specify the activity to start, A intenfilter with action Android.appwidget.action.APPWIDGET_CONFIGURE is provided under the activity in Androidmanifest.xml.

It is important to note that
If the Configure property is set, the following processing must be done in the specified activity:
1. Add Setresult (result_cancle) before the Setcontentview () function in OnCreate, so the widget does not start if the back button is pressed before activity initialization is complete;
2. After the Setcontentview () function (not necessarily in oncreate, before activity exits), add the following settings to specify which widgets need 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);

Doing so will cause the widget not to start after exiting the activity.

>> Widget Creation Steps Summary:
1. Define widget layout xml-res/layout/...
2. Define Widget Properties file (XML), res/xml/...
3. Create Appwidgetproider subclasses, implement functions such as OnUpdate (), register Receiver in manifest, add an action to Android.appwidget.action.APPWIDGET_ UPDATE the Intentfilter, and add the following <meta-data> ID:
XML code Collection Code

<meta-data android:resource= "@xml/< Properties File xml>" Android:name= "Android.appwidget.provider" ></ Meta-data>



4. Create Coniguration activity (note handling Setresult), add the Configure property to the properties file, register activity in manifest, add a Action is Android.appwidget.action.APPWIDGET_CONFIGURE's Intentfilter

Finally, a complete example is attached,
The following ideas are implemented:
1. Provide a configuration Activity, where only a single line of text is displayed;
2. The widget launches after exiting;
3. Clicking the button in the widget launches an activity

(Transferred from: http://quding0308.iteye.com/blog/1164276)

Android Widget package Description and application

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.