ANDROID_MARS学习笔记_S02_006_APPWIDGET3_AppWidget发送广播及更新AppWidget

一、简介

ANDROID_MARS学习笔记_S02_006_APPWIDGET3_AppWidget发送广播及更新AppWidget_第1张图片

 

ANDROID_MARS学习笔记_S02_006_APPWIDGET3_AppWidget发送广播及更新AppWidget_第2张图片

ANDROID_MARS学习笔记_S02_006_APPWIDGET3_AppWidget发送广播及更新AppWidget_第3张图片

二、代码
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"
 3     android:orientation="vertical"
 4     android:layout_width="fill_parent"
 5     android:layout_height="fill_parent"
 6     >
 7 <TextView  
 8     android:id="@+id/widgetTextId"
 9     android:layout_width="fill_parent" 
10     android:layout_height="wrap_content" 
11     android:text="firstWidgetText"
12     android:background="#000000"
13     />
14 <ImageView
15     android:id="@+id/imageId"
16     android:layout_width="wrap_content"
17     android:layout_height="wrap_content"
18     android:src="@drawable/xiao"
19     />
20 <Button
21     android:id="@+id/widgetButtonId"
22     android:layout_width="fill_parent"
23     android:layout_height="wrap_content"
24     android:text="测试用按钮"
25     />
26 </LinearLayout>

 

(2)AndroidManifest.xml

增加action类型以接收广播

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 package com.appwidget;
 2 
 3 import android.app.PendingIntent;
 4 import android.appwidget.AppWidgetManager;
 5 import android.appwidget.AppWidgetProvider;
 6 import android.content.ComponentName;
 7 import android.content.Context;
 8 import android.content.Intent;
 9 import android.widget.RemoteViews;
10 
11 public class ExampleAppWidgetProvider extends AppWidgetProvider{
12 
13     //定义一个常量字符串,该常量用于命名Action
14     private static final String UPDATE_ACTION = "tony.appwidget03.UPDATE_APP_WIDGET";
15     
16     // 使按钮被click时发送广播
17     @Override
18     public void onUpdate(Context context, AppWidgetManager appWidgetManager,
19             int[] appWidgetIds) {
20         //创建一个Intent对象
21         Intent intent = new Intent();
22         //为Intent对象设置Action
23         intent.setAction(UPDATE_ACTION);
24         //使用getBroadcast方法,得到一个PendingIntent对象,当该对象执行时,会发送一个广播
25         PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
26         RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.example_appwidget);
27         remoteViews.setOnClickPendingIntent(R.id.widgetButtonId, pendingIntent);
28         appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
29     }
30     
31     /**
32      * 接收广播,如果查UPDATE_ACTION,则更新文本和图片
33      * @param context
34      * @param intent
35      */
36     @Override
37     public void onReceive(Context context, Intent intent) {
38         System.out.println(UPDATE_ACTION + "----------->" + intent.getAction());
39         if(UPDATE_ACTION.equals(intent.getAction())) {
40             //获取代表此appwidget控件的remoteviews
41             RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.example_appwidget);
42             //用remoteviews设置文本、图片
43             remoteViews.setImageViewResource(R.id.imageId, R.drawable.ku);
44             remoteViews.setTextViewText(R.id.widgetTextId, "更新的文本");
45             AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
46             ComponentName componentName = new ComponentName(context, ExampleAppWidgetProvider.class);
47             appWidgetManager.updateAppWidget(componentName, remoteViews);
48         } else {
49             super.onReceive(context, intent);
50         }
51     }
52     
53     /*@Override
54     public void onUpdate(Context context, AppWidgetManager appWidgetManager,
55             int[] appWidgetIds) { //每个widget都有id
56         System.out.println("onupdate");
57         for(int i = 0; i < appWidgetIds.length ; i++) {
58             System.out.println(appWidgetIds[i]);
59             //创建一个Intent对象
60             Intent intent = new Intent(context, TargetActivity.class);
61             //创建一个PendingIntent
62             PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
63             RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.example_appwidget);
64             //为按钮绑定事件处理器
65             //第一个参数用来指定被绑定处理器的控件的ID
66             //第二个参数用来指定当事件发生时,哪个PendingIntent将会被执行
67             remoteViews.setOnClickPendingIntent(R.id.widgetButtonId, pendingIntent);
68             //更新AppWidget
69             //第一个参数用于指定被更新AppWidget的ID
70             appWidgetManager.updateAppWidget(appWidgetIds[i], remoteViews);
71         }
72         super.onUpdate(context, appWidgetManager, appWidgetIds);
73     }*/
74 
75     @Override
76     public void onDeleted(Context context, int[] appWidgetIds) {
77         System.out.println("onDeleted");
78         super.onDeleted(context, appWidgetIds);
79     }
80 
81     @Override
82     public void onDisabled(Context context) {
83         System.out.println("onDisabled");
84         super.onDisabled(context);
85     }
86 
87     @Override
88     public void onEnabled(Context context) {
89         System.out.println("onEnabled");
90         super.onEnabled(context);
91     }
92 
93     
94 }

 

(2).java

你可能感兴趣的:(ANDROID_MARS学习笔记_S02_006_APPWIDGET3_AppWidget发送广播及更新AppWidget)