Android-Widget Development

Source: Internet
Author: User

 

The Widget is a small application. After the AppWidget framework is added to Android1.5, developers can use this framework to develop widgets. Widgets can be dragged to the user's desktop and interact with each other. It provides a full-featured apps preview. You can specify a space on the desktop to display the custom content provided by the application.

Each Widget is a BroadcastReceiver, which uses XNL metadata to describe the details of the WIdget. The AppWidget framework communicates with the Widget through Broadcast intents, and the update is sent using RemotesViews. RemotesViews is encapsulated into a layout and specific content to be displayed on the desktop. The following describes how to develop a Widget using a small desktop dialing example I have found on the Internet.

Let's take a look at the structure of the entire program:

The procedure is as follows:

 

1. Create the layout file widget. xml in the res \ layout directory.

<? Xml version = "1.0" encoding = "UTF-8"?>

<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"

Android: layout_width = "wrap_content" android: layout_height = "wrap_content"

Android: orientation = "vertical" android: id = "@ + id/widLinear"

Android: focusable = "true" android: focusableInTouchMode = "true"

Android: clickable = "true" android: clipChildren = "true"

Android: background = "@ drawable/shortcallbg">

<ImageView android: layout_width = "wrap_content"

Android: layout_height = "wrap_content" android: id = "@ + id/widImageCall"

Android: layout_gravity = "center" android: layout_marginTop = "8dip"

Android: background = "@ drawable/callbtn" android: focusable = "true"

Android: clickable = "true"> </ImageView>

<TextView android: layout_width = "wrap_content"

Android: layout_height = "wrap_content" android: singleLine = "false"

Android: id = "@ + id/widNameText" android: layout_gravity = "center"

Android: width = "70dip" android: gravity = "center"

Android: textColor = "@ color/fontColor"> </TextView>

</LinearLayout>

2. Create a file named shortcallwidget. xml describing the properties of the Desktop component in the res \ xml directory,

<? Xml version = "1.0" encoding = "UTF-8"?>

<Appwidget-provider

Xmlns: android = "http://schemas.android.com/apk/res/android"

Android: initialLayout = "@ layout/widget"

Android: minWidth = "72dip" android: minHeight = "72dip"

Android: updatePeriodMillis = "0"

Android: configure = "com. ty. shortcall. ConfiguresActivity">

</Appwidget-provider>

Android: updatePeriodMillis = "0" indicates the automatic update interval. Android: configure is optional. If your Widget needs to start an Activity before it starts, set this item to your Activity.

3. create a ShortCallWidget class to inherit from AppWidgetProvider. appwidgetProvider has many methods, such as OnUpdate, OnDelete, and OnEnabled ), developers can rewrite these methods as needed.

Public void onUpdate (Context context, AppWidgetManager appWidgetManager,

Int [] appWidgetIds ){

// TODO Auto-generated method stub

Super. onUpdate (context, appWidgetManager, appWidgetIds );

Log. d (TAG, "login the widget ");

Int count = appWidgetIds. length;

For (int I = 0; I <count; I ++ ){

Int widgetId = appWidgetIds [I];

UpdateAppWidget (context, appWidgetManager, widgetId );

}

}

 

4. process the class specified by android: configure, listen to the saved button in the class, click the button, create an AppWidgetManager instance, and update the Widget by calling the previous. updateAppWidget method.

5. Do not forget to register the ShortCallWidget and ConfiguresActivity classes in AndroidMainfest. xml.

:

Effect after clicking:

 

This article is from the "HDDevTeam" blog

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.