Android Application Development (10): Use of widgets and androidwidgets

Source: Internet
Author: User

Android Application Development (10): Use of widgets and androidwidgets

1. Custom widgets must inherit the AppWidgetProvider

Source code: http://www.jinhusns.com/Products/Download? Type = xcj

 

 2. AndroidManifest. xml must be registered

 

<Cycler android: name = ". widget. AppWidget">

 

<Meta-data

 

Android: name = "android. appwidget. provider"

 

Android: resource = "@ xml/guide_widget"/>

 

<Intent-filter>

 

<Action android: name = "android. appwidget. action. APPWIDGET_UPDATE"/>

 

</Intent-filter>

 

</Cycler>

 

(1) the consumer er name must be the custom widget class name.

 

(2) the meta-data name must be android. appwidget. provider.

 

(3) the resource of meta-data is the description file of the widget, which must be placed in the res/xml Path.

 

(4) You need to add action to add the listener handler type android. appwidget. action. APPWIDGET_UPDATE.

 

3. res/xml

 

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

 

<Appwidget-provider xmlns: android = "http://schemas.android.com/apk/res/android"

 

Android: initialLayout = "@ layout/guide_widget"

 

Android: minHeight = "294.0dip"

 

Android: minWidth = "294.0dip"

 

Android: updatePeriodMillis = "1801000" type = "codeph" text = "/codeph"/>

 

(1) The outermost tag must be appwidget-provider.

 

(2) android: initialLayout specifies the layout file used by the widget.

 

(3) android: minHeight specifies the widget height, android: minWidth specifies the widget width. These two values cannot be specified at will. It is best to set them based on the number of rows and columns occupied by widgets to be placed. For example, if a widget contains three rows and two columns, minHeight should be (3*74)-2 = 220, and minWidth should be (2*74)-2 = 146

 

(4) android: updatePeriodMillis. The value is the refresh time of the widget. It is best to set it to more than one hour. Otherwise, system resources will be seriously wasted and power consumption will be high. At a fixed time, the system calls the onUpdate method of the widget. If the value is 0, the system does not update the widget.

 

  4. widgets inherit from parent class methods

 

(1) onEnabled (Context context)

 

This method will be called after the user adds a widget

 

(2) onUpdate (Context context, AppWidgetManager

 

AppWidgetManager, int [] appWidgetIds)

 

This method is called after the user adds a widget to refresh the installed widget on the interface (called after onEnabled ).

 

The appWidgetManager parameter is used to refresh the interface.

 

AppWidgetIds is the id of all widgets of the application (You may add Multiple widgets, so this is an array)

 

(3) onDeleted (Context context, int [] appWidgetIds)

 

This method will be called after the widget is deleted (Note: After the widget is deleted, that is, this method is not used to delete the widget)

 

(4) onReceive (Context context, Intent intent)

 

This method is inherited from the aggreger and should not be used as much as possible. If you want to use it, you need to handle some special code yourself. Otherwise, the widget may have problems.

 

5. Page widget refresh and click events

 

(1) In widgets, you cannot use the findviewbyid method to obtain components. You need to use other methods. RemoteViews rv = new RemoteViews (context. getPackageName (),

 

R. layout. guide_widget );

 

RemoteViews is a fictitious component that carries layout.

 

(2) Click a component in layout to start activity or service.

 

Intent intent = new Intent (context, WidgetDemoActivity. class );

 

PendingIntent pendingIntent = PendingIntent. getActivity (context, 0, intent, 0 );

 

Rv. setOnClickPendingIntent (R. id. guide_0, pendingIntent );

 

  (3) interface refresh

 

AppWidgetManager. updateAppWidget (appWidgetId, rv );

 

6. widgets support components used in layout.

 

FrameLayout, LinearLayout, RelativeLayout

 

AnalogClock, Button, Chronometer, ImageButton, ImageView, ProgressBar, TextView

 

7. Complete custom widget class code

 

Public class AppWidget extends AppWidgetProvider {

 

RemoteViews rv;

 

@ Override

 

Public void onUpdate (Context context, AppWidgetManager appWidgetManager,

 

Int [] appWidgetIds ){

 

System. out. println (context. getPackageName ());

 

Final int N = appWidgetIds. length;

 

// Because you may have added Multiple widgets, You need to traverse the appWidgetIds

 

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

 

System. out. println (appWidgetIds [I]);

 

Int appWidgetId = appWidgetIds [I];

 

Rv = new RemoteViews (context. getPackageName (),

 

R. layout. guide_widget );

 

Intent intent = new Intent (context, WidgetDemoActivity. class );

 

Intent. setAction (context. getPackageName () + appWidgetId );

 

PendingIntent pendingIntent = PendingIntent. getActivity (context, 0,

 

Intent, 0 );

 

Rv. setOnClickPendingIntent (R. id. guide_0, pendingIntent );

 

AppWidgetManager. updateAppWidget (appWidgetId, rv );

 

}

 

Super. onUpdate (context, appWidgetManager, appWidgetIds );

 

}

 

}

 

8. widget lifecycle (android1.6)

 

(1) When you drag a widget to the interface, the following methods are called in order: onReceive-onEnabled-onReceive-onUpdate-onReceive

 

(2) When you drag a widget to delete it, the following methods are called in order:

 

OnReceive-onDeleted-onReceive-onDisabled

Free training courses: http://www.jinhusns.com/Products/Curriculum? Type = xcj

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.