widget必须继承AppWidgetProvider
在AppWidgetProvider源码中可以看到AppWidgetProvider继承了BroadcastReceiver
也就是说widget本身就是个BroadcastReceiver(广播接受者)
AndroidManifest.xml中配置
<receiver android:name="ExampleAppWidgetProvider" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data android:name="android.appwidget.provider"
android:resource="@xml/example_appwidget_info" />
</receiver>
当往窗口添加widget是会发出更新广播
更新广播的名称为"android.appwidget.action.APPWIDGET_UPDATE"
meta-data为组件提供参数
android:name="android.appwidget.provider"为固定的
android:resource="@xml/example_appwidget_info"为widght所采用的界面
example_appwidget_info.xml代码
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="294dp"
android:minHeight="72dp"
android:updatePeriodMillis="0"
android:initialLayout="@layout/time_appwidget">
</appwidget-provider>
time_appwidget.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:orientation="vertical"
android:background="@drawable/rectangle" >
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/textView"
android:textColor="@android:color/black"
android:textSize="20dp"
android:text="Just For Test"/>
</LinearLayout>
一个简单的wdiget,只有一句话"Just For Test"
接下来改进一下,把文字换成时间
public class TimeWidgetProvider extends AppWidgetProvider {
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String time = dateFormat.format(System.currentTimeMillis());
RemoteViews remoteView = new RemoteViews(context.getPackageName(), R.layout.time_appwidget);
remoteView.setTextViewText(R.id.textView, time);
appWidgetManager.updateAppWidget(appWidgetIds[0], remoteView);
}
}
部署后时间出来了,但是时间是固定的,没有改变,需要加入服务
新建服务
package com.ice.widght;
import java.text.SimpleDateFormat;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.Intent;
import android.os.IBinder;
import android.widget.RemoteViews;
public class TimeWidgetService extends Service {
private Timer timer;
@Override
public void onCreate() {
timer = new Timer();
timer.schedule(new TimeUpdateTask(), 0, 1000);
}
private class TimeUpdateTask extends TimerTask{
@Override
public void run() {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String time = dateFormat.format(System.currentTimeMillis());
RemoteViews remoteView = new RemoteViews(getPackageName(), R.layout.time_appwidget);
remoteView.setTextViewText(R.id.textView, time);
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(getApplicationContext());
appWidgetManager.updateAppWidget(
new ComponentName(getApplicationContext(), TimeWidgetProvider.class), remoteView);
}
}
@Override
public void onDestroy() {
timer.cancel();
timer = null;
}
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
}
TimeWidgetProvider重构代码,负责开启和销毁服务
public class TimeWidgetProvider extends AppWidgetProvider {
@Override
public void onEnabled(Context context) {
context.startService(new Intent(context, TimeWidgetService.class));
}
@Override
public void onDisabled(Context context) {
context.stopService(new Intent(context, TimeWidgetService.class));
}
}
完工~~~