新建一个类继承AppWidgetProvider重写几个重要的方法,代码如下:
<pre name="code" class="java">package com.example.texttest;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class Widget extends AppWidgetProvider {
@Override
public void onDeleted(Context context, int[] appWidgetIds) {
super.onDeleted(context, appWidgetIds);
context.stopService(new Intent(context, widgetService.class));//停止Service
}
@Override
public void onDisabled(Context context) {
super.onDisabled(context);
context.stopService(new Intent(context, widgetService.class)); //停止Service
Log.e("", "最后一个被删除了");
}
@Override
public void onEnabled(Context context) {
super.onEnabled(context);
context.startService(new Intent(context, widgetService.class));//启动Service
Log.e("", "增加上屏幕");
}
@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
// TODO Auto-generated method stub
super.onUpdate(context, appWidgetManager, appWidgetIds);
Log.e("", "更新");
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="你好 "
android:id="@+id/widget_tv"
android:textColor="#ffaaaa"
android:textSize="30dp" />
</RelativeLayout>
新建一个XML文件夹存放Widget的配制文件:
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:initialLayout="@layout/widget"
android:minHeight="40dp"
android:minWidth="180dp"
android:updatePeriodMillis="8000" >
<!-- initialLayout 找到Widget的布局, minHeight,minWidth 最小宽高 。updatePeriodMillis 更新时间,默认3分钟,小于3分钟默认三分钟-->
</appwidget-provider>
在AndroidManifast里注册:
<service android:name="com.example.texttest.widgetService" >
</service>
<receiver android:name="com.example.texttest.Widget" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/widget_layout" />
</receiver>
写一个Service
package com.example.texttest;
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 widgetService extends Service {
private int i = 0;
private Timer time;
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
time = new Timer();
time.schedule(new TimerTask() {
@Override
public void run() {
updateView();
}
}, 0, 1000); // 1000是间隔时间,0是延迟
}
/**
* 更新UI
* @param getApplicationContext() 上下文
*/
private void updateView() {
i++;
RemoteViews re = new RemoteViews(getPackageName(), R.layout.widget);// 找到Widget的布局
re.setTextViewText(R.id.widget_tv, "你好" + i); // 设置内容
AppWidgetManager appwm = AppWidgetManager
.getInstance(getApplicationContext()); // 获得WidgetManager
ComponentName com = new ComponentName(getApplicationContext(),
Widget.class); // 设置widget的类
appwm.updateAppWidget(com, re); //
}
@Override
public void onDestroy() {
super.onDestroy();
time = null; //清空time
}
}