最近学习了一下AppWidget,写个小Demo,记录下 。大概效果如下图:

Demo的结构(MainActivity为默认代码)

1.清单文件配置广播接收者和一个服务
<receiver
android:name=".MyAppWidgetProvider"
android:permission="com.android.launcher.permission.INSTALL_SHORTCUT">
<intent-filter>
<action android:name="com.terry.action.widget.click" />
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/widget_provider"/>
</receiver>
<service android:name=".UpdateSecService"/>
2.在res下新建xml文件,创建一个xml,创建widget_provider.xml(与清单文件配置一致)

widget_provider.xml代码:
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:initialLayout="@layout/process_widget"
android:minHeight="72.0dip"
android:minWidth="294.0dip" />
3.在layout下,创建process_widget.xml(名字与widget_provider.xml配置一致)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/ll_root"
android:layout_width="match_parent"
android:layout_height="80dp"
android:background="#338bcfe7">
<Button
android:id="@+id/btn_start_activity"
android:layout_width="90dp"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="15.0dip"
android:background="#60b183"
android:text="开启Activity"
android:textColor="#fff"
tools:ignore="RtlCompat" />
<Button
android:id="@+id/btn_start_service"
android:layout_width="90dp"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="15.0dip"
android:background="#ed7676"
android:text="开启Service"
android:textColor="#fff"
tools:ignore="RtlCompat" />
<TextView
android:id="@+id/tv_service"
android:layout_width="90dp"
android:layout_height="50dp"
android:layout_gravity="center_vertical"
android:layout_marginStart="5dp"
android:gravity="center_vertical"
android:text="服务开启:0秒"
tools:ignore="RtlCompat" />
</LinearLayout>
4.创建服务UpdateSecService
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.util.Log;
import android.widget.RemoteViews;
public class UpdateSecService extends Service {
protected static final String tag = "UpdateSecService";
private Timer mTimer;
@Override
public void onCreate() {
super.onCreate();
//开启定时器
startTimer();
}
private void startTimer() {
mTimer = new Timer();
mTimer.scheduleAtFixedRate(new TimerTask() {
int i = 1 ;
@Override
public void run() {
//ui定时刷新
updateAppWidget(i);
i+=1;
Log.i(tag, "1秒一次的定时任务现在正在运行..........");
}
}, 0, 1000);
}
protected void updateAppWidget(int i) {
//获取AppWidget对象
AppWidgetManager aWM = AppWidgetManager.getInstance(this);
//获取窗体小部件布局转换成的view对象(定位应用的包名,当前应用中的那块布局文件)
RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.process_widget);
//给窗体小部件布view对象,内部控件赋值
remoteViews.setTextViewText(R.id.tv_service, "服务开启:" + i + "秒");
//上下文环境,窗体小部件对应广播接受者的字节码文件
ComponentName componentName = new ComponentName(this,MyAppWidgetProvider.class);
//更新窗体小部件
aWM.updateAppWidget(componentName, remoteViews);
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onDestroy() {
super.onDestroy();
cancelTimerTask();
}
public void cancelTimerTask() {
//mTimer中cancel方法取消定时任务方法
if(mTimer!=null){
mTimer.cancel();
mTimer = null;
}
}
}
5.创建广播接收者MyAppWidgetProvider
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.RemoteViews;
public class MyAppWidgetProvider extends AppWidgetProvider {
private static final String tag = "MyAppWidgetProvider";
@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
Log.i(tag, "onReceive............");
}
@Override
public void onEnabled(Context context) {
super.onEnabled(context);
//创建第一个窗体小部件的方法
Log.i(tag, "onEnabled 创建第一个窗体小部件调用方法");
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
Log.i(tag, "onUpdate 创建多一个窗体小部件调用方法");
//开启Activity
//获取窗体小部件布局转换成的view对象(定位应用的包名,当前应用中的那块布局文件)
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.process_widget);
//创建一个Intent对象
Intent intent = new Intent(context, MainActivity.class);
//设置pendingIntent的作用
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
//给窗体小部件布view对象,内部控件赋值
remoteViews.setOnClickPendingIntent(R.id.btn_start_activity, pendingIntent);
Intent intent1 = new Intent(context, UpdateSecService.class);
PendingIntent pendingIntent1 = PendingIntent.getService(context, 0, intent1, PendingIntent.FLAG_CANCEL_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.btn_start_service, pendingIntent1);
//更新窗体小部件
appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
}
@Override
public void onAppWidgetOptionsChanged(Context context, AppWidgetManager appWidgetManager, int appWidgetId, Bundle newOptions) {
super.onAppWidgetOptionsChanged(context, appWidgetManager, appWidgetId, newOptions);
//当窗体小部件宽高发生改变的时候调用方法,创建小部件的时候,也调用此方法
Log.i(tag, "onAppWidgetOptionsChanged 创建多一个窗体小部件调用方法");
}
@Override
public void onDeleted(Context context, int[] appWidgetIds) {
super.onDeleted(context, appWidgetIds);
Log.i(tag, "onDeleted 删除一个窗体小部件调用方法");
}
@Override
public void onDisabled(Context context) {
super.onDisabled(context);
Log.i(tag, "onDisabled 删除最后一个窗体小部件调用方法");
}
}
这样就完成了。
网友评论