First, Introduction
Second, the Code
1.xml
(1) Example_appwidget.xml
1<?xml version= "1.0" encoding= "Utf-8"?>2<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"3android:orientation= "Vertical"4Android:layout_width= "Fill_parent"5android:layout_height= "Fill_parent"6>7<TextView8Android:id= "@+id/widgettextid"9Android:layout_width= "Fill_parent"Tenandroid:layout_height= "Wrap_content" Oneandroid:text= "Firstwidgettext" AAndroid:background= "#000000" -/> -<ImageView theAndroid:id= "@+id/imageid" -Android:layout_width= "Wrap_content" -android:layout_height= "Wrap_content" -android:src= "@drawable/xiao" +/> -<Button +Android:id= "@+id/widgetbuttonid" AAndroid:layout_width= "Fill_parent" atandroid:layout_height= "Wrap_content" -android:text= "Test Button" -/> -</LinearLayout>
(2) Androidmanifest.xml
Increase the action type to receive the broadcast
1 <receiver android:name= "Exampleappwidgetprovider" >2 <intent-filter>3 < Action android:name= "Android.appwidget.action.APPWIDGET_UPDATE"/>4 <action android:name= " Tony.appwidget03.UPDATE_APP_WIDGET "/>5 </intent-filter>6 <meta-data Android:name= "Android.appwidget.provider" android:resource= "@xml/example_appwidget_info"/>7 8 </receiver>
2.java
(1) Exampleappwidgetprovider.java
1 PackageCom.appwidget;2 3 Importandroid.app.PendingIntent;4 ImportAndroid.appwidget.AppWidgetManager;5 ImportAndroid.appwidget.AppWidgetProvider;6 ImportAndroid.content.ComponentName;7 ImportAndroid.content.Context;8 Importandroid.content.Intent;9 Importandroid.widget.RemoteViews;Ten One Public classExampleappwidgetproviderextendsappwidgetprovider{ A - //defines a constant string that is used to name the action - Private Static FinalString update_action = "Tony.appwidget03.UPDATE_APP_WIDGET"; the - //send a broadcast when the button is clicked - @Override - Public voidOnUpdate (Context context, Appwidgetmanager Appwidgetmanager, + int[] appwidgetids) { - //Create a Intent object +Intent Intent =NewIntent (); A //set action for intent object at intent.setaction (update_action); - //using the Getbroadcast method, a Pendingintent object is obtained, and when the object executes, a broadcast is sent -Pendingintent pendingintent = pendingintent.getbroadcast (context, 0, intent, 0); -Remoteviews remoteviews =Newremoteviews (Context.getpackagename (), r.layout.example_appwidget); - remoteviews.setonclickpendingintent (R.id.widgetbuttonid, pendingintent); - Appwidgetmanager.updateappwidget (Appwidgetids, remoteviews); in } - to /** + * Receive broadcasts, update text and pictures if update_action is checked - * @paramContext the * @paramIntent * */ $ @OverridePanax Notoginseng Public voidOnReceive (Context context, Intent Intent) { -System.out.println (update_action + "----------->" +intent.getaction ()); the if(Update_action.equals (Intent.getaction ())) { + //gets the remoteviews that represents this Appwidget control ARemoteviews remoteviews =Newremoteviews (Context.getpackagename (), r.layout.example_appwidget); the //set text, picture with Remoteviews + Remoteviews.setimageviewresource (R.id.imageid, r.drawable.ku); -Remoteviews.settextviewtext (R.id.widgettextid, "Updated text"); $Appwidgetmanager Appwidgetmanager =appwidgetmanager.getinstance (context); $ComponentName componentname =NewComponentName (context, Exampleappwidgetprovider.class); - Appwidgetmanager.updateappwidget (componentname, remoteviews); -}Else { the Super. OnReceive (context, intent); - }Wuyi } the - /*@Override Wu Public void OnUpdate (context context, Appwidgetmanager Appwidgetmanager, - int[] appwidgetids) {//each widget has an ID About System.out.println ("OnUpdate"); $ for (int i = 0; i < appwidgetids.length; i++) { - System.out.println (Appwidgetids[i]); - //Create a Intent object - Intent Intent = new Intent (context, targetactivity.class); A //Create a pendingintent + pendingintent pendingintent = pendingintent.getactivity (context, 0, intent, 0); the remoteviews remoteviews = new Remoteviews (Context.getpackagename (), r.layout.example_appwidget); - //Bind event handler for button $ //The first parameter is used to specify the ID of the control that is bound to the processor the The second parameter is used to specify which Pendingintent will be executed when an event occurs. the remoteviews.setonclickpendingintent (R.id.widgetbuttonid, pendingintent); the //Update Appwidget the //The first parameter specifies the ID of the updated appwidget - Appwidgetmanager.updateappwidget (Appwidgetids[i], remoteviews); in } the super.onupdate (context, Appwidgetmanager, appwidgetids); the }*/ About the @Override the Public voidOndeleted (Context context,int[] appwidgetids) { theSystem.out.println ("ondeleted"); + Super. ondeleted (context, appwidgetids); - } the Bayi @Override the Public voidondisabled (Context context) { theSystem.out.println ("ondisabled"); - Super. ondisabled (context); - } the the @Override the Public voidonenabled (Context context) { theSystem.out.println ("onenabled"); - Super. onenabled (context); the } the the 94}
(2). java
Android_mars Study notes _s02_006_appwidget3_appwidget send broadcast and update appwidget