ANDROID_MARS学习笔记_S02_006_APPWIDGET2_PendingIntent及RemoteViews实现widget绑定点击事件

一、代码流程

1.ExampleAppWidgetProvider的onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds),其中appWidgetIds表示widget的id,

在这个方法里,通过for遍历appWidgetIds,用PendingIntent和RemoteViews给widget的按钮绑定click事件,用appWidgetManager.updateAppWidget(appWidgetIds[i], remoteViews)更新widget

 

二、PendingIntent及RemoteViews简介

ANDROID_MARS学习笔记_S02_006_APPWIDGET2_PendingIntent及RemoteViews实现widget绑定点击事件_第1张图片

ANDROID_MARS学习笔记_S02_006_APPWIDGET2_PendingIntent及RemoteViews实现widget绑定点击事件_第2张图片

ANDROID_MARS学习笔记_S02_006_APPWIDGET2_PendingIntent及RemoteViews实现widget绑定点击事件_第3张图片ANDROID_MARS学习笔记_S02_006_APPWIDGET2_PendingIntent及RemoteViews实现widget绑定点击事件_第4张图片

ANDROID_MARS学习笔记_S02_006_APPWIDGET2_PendingIntent及RemoteViews实现widget绑定点击事件_第5张图片

三、代码
1.xml
(1)AndroidManifest.xml

添加activity

1 <activity android:name=".TargetActivity" android:label="TargetActivityLabel"/>

(2)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 <Button
15     android:id="@+id/widgetButtonId"
16     android:layout_width="fill_parent"
17     android:layout_height="wrap_content"
18     android:text="测试用按钮"
19     />
20 </LinearLayout>

 

2.java
(1)ExampleAppWidgetProvider.java的update

 1 @Override
 2     public void onUpdate(Context context, AppWidgetManager appWidgetManager,
 3             int[] appWidgetIds) { //每个widget都有id
 4         System.out.println("onupdate");
 5         for(int i = 0; i < appWidgetIds.length ; i++) {
 6             System.out.println(appWidgetIds[i]);
 7             //创建一个Intent对象
 8             Intent intent = new Intent(context, TargetActivity.class);
 9             //创建一个PendingIntent
10             PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
11             RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.example_appwidget);
12             //为按钮绑定事件处理器
13             //第一个参数用来指定被绑定处理器的控件的ID
14             //第二个参数用来指定当事件发生时,哪个PendingIntent将会被执行
15             remoteViews.setOnClickPendingIntent(R.id.widgetButtonId, pendingIntent);
16             //更新AppWidget
17             //第一个参数用于指定被更新AppWidget的ID
18             appWidgetManager.updateAppWidget(appWidgetIds[i], remoteViews);
19         }
20         super.onUpdate(context, appWidgetManager, appWidgetIds);
21     }

 

(2)TargetActivity.java

1 public class TargetActivity extends Activity {
2 
3     @Override
4     protected void onCreate(Bundle savedInstanceState) {
5         System.out.println("TargetActivity onCreate()----->");
6         super.onCreate(savedInstanceState);
7     }
8 }

 

你可能感兴趣的:(ANDROID_MARS学习笔记_S02_006_APPWIDGET2_PendingIntent及RemoteViews实现widget绑定点击事件)