Application Widget lecture 1 (Android Learning Guide)

Source: Internet
Author: User

 

Steps for implementing the App Widget:

 

A. Create an AppWidgetProvider object and define it by using XML (appwidgetinfo. xml:

 

View

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

<Appwidget-provider

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

Android: minWidth = "294dp"

Android: minHeight = "72dp"

Android: updatePeriodMillis = "86400000"

Android: initialLayout = "@ layout/awl"

>

</Appwidget-provider>

Defines the minimum width, minimum height, update period, and initial layout of the AppWidget.

 

The initial layout file (awl. xml) of the AppWidget is as follows:

 

View

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

<LinearLayout

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

Android: layout_width = "fill_parent"

Android: layout_height = "fill_parent"

Android: orientation = "vertical">

<TextView android: id = "@ + id/TV"

Android: layout_width = "fill_parent"

Android: layout_height = "wrap_content"

Android: text = "test"/>

<Button android: id = "@ + id/btn"

Android: layout_width = "fill_parent"

Android: layout_height = "wrap_content"

Android: text = "click"/>

</LinearLayout>

B. Implement the AppWidgetProvider class (AWTProvider. java ):

 

View

Package yc. yc;

 

Import android. app. PendingIntent;

Import android. appwidget. AppWidgetManager;

Import android. appwidget. AppWidgetProvider;

Import android. content. Context;

Import android. content. Intent;

Import android. widget. RemoteViews;

 

Public class AWTProvider extends AppWidgetProvider {

 

// Every time an AppWidget is deleted, the onDeleted method is called.

@ Override

Public void onDeleted (Context context, int [] appWidgetIds ){

System. out. println ("delete ");

Super. onDeleted (context, appWidgetIds );

}

 

// When the last AppWidget is deleted, the onDisabled method is called.

// In this method, terminate some AppWidget operations, such as deleting a temporary database.

@ Override

Public void onDisabled (Context context ){

System. out. println ("disable ");

Super. onDisabled (context );

}

// When the first AppWidget instance is created, the onEnabled method is called.

// In this method, initialize some AppWidget, such as creating a database or performing some operations.

@ Override

Public void onEnabled (Context context ){

System. out. println ("enable ");

Super. onEnabled (context );

}

// OnReceive is equivalent to a manager. When an external task comes, the task is distributed to its employees (onEnable, onDeleted, onDisabled, onUpdate) for processing.

@ Override

Public void onReceive (Context context, Intent intent ){

System. out. println ("receive ");

Super. onReceive (context, intent );

}

 

// Here, there are two conditions for onUpdate execution: 1. The update cycle is set; 2. Add a new AppWidget.

// In onUpdate, event processing is defined for the control or a temporary Service is enabled.

// Here is how to add a click event for our button control.

// Context indicates the environment in which the receiver is running

// AppWManager indicates that the AppWidget manager can change the control attributes of a specified AppWidget by calling updateAppWidget (ComponentName, RemoteViews ).

// AppWidgetIds indicates the Appwidget ID, which is automatically generated by the system.

@ Override

Public void onUpdate (Context context, AppWidgetManager appWidgetManager,

Int [] appWidgetIds ){

System. out. println ("update ");

Final int N = appWidgetIds. length;

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

Int appWidgetId = appWidgetIds [I];

System. out. println ("appWidgetId =" + appWidgetId );

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

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

RemoteViews views = new RemoteViews (context. getPackageName (), R. layout. awl );

Views. setOnClickPendingIntent (R. id. btn, pendingIntent );

AppWidgetManager. updateAppWidget (appWidgetId, views );

}

}

 

}

C. register the implementation class in AndroidManifest. xml and define metadata:

 

View

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

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

Package = "yc. yc"

Android: versionCode = "1"

Android: versionName = "1.0" type = "codeph" text = "/codeph">

<Uses-sdk android: minSdkVersion = "7"/>

 

<Application android: icon = "@ drawable/icon" android: label = "@ string/app_name">

<Activity android: name = ". AWTActivity"

Android: label = "@ string/app_name">

<Intent-filter>

<Action android: name = "android. intent. action. MAIN"/>

<Category android: name = "android. intent. category. LAUNCHER"/>

</Intent-filter>

</Activity>

<! -- Register the implementation class -->

<Explorer android: name = ". AWTProvider">

<Intent-filter>

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

</Intent-filter>

<! -- Define metadata -->

<Meta-data android: name = "android. appwidget. provider"

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

</Cycler>

</Application>

</Manifest>

 

Note: This is only the first lecture of AppWidget and the subsequent courses.

 



From tianshijianbing1989

 

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.