一、简介
桌面小部件,是android提供支持的一种特殊广播,允许在桌面以插件的形式展示内容。
二、示例
1、创建布局文件:此处的布局只使用一个简单的TextView
/layout/layout_widget.xml
<?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:gravity="center" android:orientation="vertical" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="0000-00-00 00:00:00" android:textColor="@android:color/background_dark" > </TextView></LinearLayout>
2、创建AppWidgetProvider的子类
import java.text.SimpleDateFormat;import java.util.Date;import android.app.AlarmManager;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.widget.RemoteViews;import com.am.appwidget.R;public class TimeWidgetProvider extends AppWidgetProvider { private final String UPDATE_ACTION = "TimeWidgetProvider.update.date"; // 收到请求时执行 @Override public void onReceive(Context context, Intent intent) { System.out.println("===onReceive"); if (intent != null && UPDATE_ACTION.equals(intent.getAction())) { updateViews(context); } super.onReceive(context, intent); } @Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { System.out.println("===onUpdate"); // 设置定时任务,定时刷新widget StartAlarm(context); } // 删除时调用 @Override public void onDeleted(Context context, int[] appWidgetIds) { System.out.println("===onDeleted"); stopAlarm(context); } private void StartAlarm(Context context) { Intent intent = new Intent(context, TimeWidgetProvider.class); intent.setAction(UPDATE_ACTION); // AppWidgetProvider 是 BroadcastReceiver 的子类 PendingIntent refreshIntent = PendingIntent.getBroadcast(context, 0, intent, 0); AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); alarm.setRepeating(AlarmManager.RTC, 0, 1000, refreshIntent); } private void stopAlarm(Context context) { Intent intent = new Intent(context, TimeWidgetProvider.class); intent.setAction(UPDATE_ACTION); PendingIntent refreshIntent = PendingIntent.getBroadcast(context, 0, intent, 0); AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); alarm.cancel(refreshIntent); } private void updateViews(Context context) { // 只能通过远程对象来设置appwidget中的控件状态 SimpleDateFormat sf = new SimpleDateFormat("yyy-MM-dd HH:mm:ss"); String date = sf.format(new Date()); RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.layout_widget); remoteViews.setTextViewText(R.id.textView1, date); // 获得appwidget管理实例,用于管理appwidget以便进行更新操作 AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context); // 相当于获得所有本程序创建的appwidget ComponentName componentName = new ComponentName(context, TimeWidgetProvider.class); // 更新appwidget appWidgetManager.updateAppWidget(componentName, remoteViews); }}3、创建Appwidget的配置文件
/xml/provider_config.xml
<?xml version="1.0" encoding="utf-8"?><appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" android:initialLayout="@layout/layout_widget" android:minHeight="40dp" android:minWidth="80dp" android:updatePeriodMillis="0" > <!-- updatePeriodMillis:官网的意思如果更新频率大于一次/小时会造成电池寿命问题,设置小了也不起作用,所以在此使用0也就是不更新,后台使用定时任务刷新 --></appwidget-provider>4、配置AndroidManifest.xml
<application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > ... ... <!-- 配置AppWidgetProvider --> <receiver android:name="com.am.appwidget.widget.TimeWidgetProvider" android:label="Time App Widget" > <!-- 必须显示声明以下action --> <intent-filter> <action android:name="android.appwidget.action.APPWIDGET_UPDATE" > </action> </intent-filter> <!-- 指定widget的配置文件路径 --> <meta-data android:name="android.appwidget.provider" android:resource="@xml/provider_config" /> </receiver> </application>
三、结果