这个例子里面我们实现一个AppWidget,上面放置一个广播按钮,按下后可以广播事件,同时这个widget可以接收到这个广播。
AndroidManifest.xml文件内容如下:
<receiver android:name=".appwidget.FreeWidgetProvider" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<intent-filter>
<action android:name="com.freesoft.framework.appwidget.FreeWidgetProvider" />
</intent-filter>
<meta-data android:name="android.appwidget.provider"
android:resource="@xml/free_appwidget_info" />
</receiver>
注意以上有两个intent-filter,第二个过滤了一个自定义的事件消息。
元数据xml文件如下:
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minHeight="72dp"
android:minWidth="294dp"
android:updatePeriodMillis="86400000"
android:initialLayout="@layout/free_appwidget" >
</appwidget-provider>
布局文件如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button android:id="@+id/btn_faw_testbtn"
android:text="@string/s_btn_faw_testbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView android:id="@+id/tv_faw_testview"
android:text="@string/test_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<Button android:id="@+id/btn_faw_broadcastbtn"
android:text="@string/s_btn_faw_broadcastbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
Provider类如下:
package com.freesoft.frameworkdemo.appwidget;
import com.freesoft.frameworkdemo.R;
import com.freesoft.frameworkdemo.activity.ActivityDemoActivity;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.RemoteViews;
public class FreeWidgetProvider extends AppWidgetProvider {
private static final String TAG="FreeWidgetProvider";
private static final String BROADCAST_STRING="com.freesoft.framework.appwidget.FreeWidgetProvider";
@Override
public void onDeleted(Context context, int[] appWidgetIds) {
Log.v(TAG, "onDeleted");
super.onDeleted(context, appWidgetIds);
}
@Override
public void onDisabled(Context context) {
Log.v(TAG, "onDisabled");
super.onDisabled(context);
}
@Override
public void onEnabled(Context context) {
Log.v(TAG, "onEnabled");
super.onEnabled(context);
}
@Override
public void onReceive(Context context, Intent intent) {
Log.v(TAG, "onReceive");
if (intent.getAction().equals(BROADCAST_STRING)) {
Log.v(TAG, "BROADCAST_STRING");
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.free_appwidget);
views.setTextViewText(R.id.tv_faw_testview, "刷新后的文字");
AppWidgetManager awm = AppWidgetManager.getInstance(context);
ComponentName cn = new ComponentName(context, FreeWidgetProvider.class);
awm.updateAppWidget(cn, views);
}
super.onReceive(context, intent);
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
Log.v(TAG, "onUpdate");
for (int i = 0; i < appWidgetIds.length; i++) {
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.free_appwidget);
Intent intent = new Intent(context, ActivityDemoActivity.class);
PendingIntent pi = PendingIntent.getActivity(context, 0, intent, 0);
views.setOnClickPendingIntent(R.id.btn_faw_testbtn, pi);
Intent bintent = new Intent();
bintent.setAction(BROADCAST_STRING);
PendingIntent bpi = PendingIntent.getBroadcast(context, 0, bintent, 0);
views.setOnClickPendingIntent(R.id.btn_faw_broadcastbtn, bpi);
appWidgetManager.updateAppWidget(appWidgetIds[i], views);
}
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
}
注意这里首先覆盖了onReceive方法,并在方法中处理广播事件。
而onUpdate方法中注册了按钮的广播事件,并设置一个PendingIntent。
PendingIntent和Intent:
Intent即意图,标识我们期望执行的动作。
PendingIntent即未发生的意图,表示需要一定的条件触发。实际上PendingIntent需要通过getXXX,诸如intent(s)、broadcast或service来创建,而不能通过new一个PendingIntent来创建。