1.在layout中创建文件my_appwidget.xml布局文件,就是显示的小部件的窗口:
按照需求布局即可
2.在xml下创建文件my_appwidget_info.xml指定minWidth="40dp"minHeight="40dp"的属性:
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="40dp"
android:minHeight="40dp"
android:updatePeriodMillis="86400000"
android:initialLayout="@layout/my_appwidget"
>
</appwidget-provider>
3.由于小部件不依赖于Activity而存在,因此小部件内容的更新需要放置在服务中进行:
因此我们需要在MyAppWidgetProvider中重写AppWidgetProvider,用来接受与widget相关的删除,失效,生效和更新等消息,然后重写,并开启服务:
/**
* 连接小部件
* @param context
*/
@Override
public void onEnabled(Context context) {
context.startService(new Intent(context,UpDateWidgeService.class));
super.onEnabled(context);
}
/**
* 更新创建小部件
* @param context
* @param appWidgetManager
* @param appWidgetIds
*/
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
context.startService(new Intent(context,UpDateWidgeService.class));
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
/**
*
* @param context
* @param appWidgetManager
* @param appWidgetId
* @param newOptions
*/
@Override
public void onAppWidgetOptionsChanged(Context context, AppWidgetManager appWidgetManager, int appWidgetId, Bundle newOptions) {
context.startService(new Intent(context,UpDateWidgeService.class));
super.onAppWidgetOptionsChanged(context, appWidgetManager, appWidgetId, newOptions);
}
/**
* 删除小部件
* @param context
* @param appWidgetIds
*/
@Override
public void onDeleted(Context context, int[] appWidgetIds) {
super.onDeleted(context, appWidgetIds);
}
/**
* 删除到最后一个小部件时,销毁连接
* @param context
*/
@Override
public void onDisabled(Context context) {
context.stopService(new Intent(context,UpDateWidgeService.class));
super.onDisabled(context);
}
4.开启服务后需要在服务中控制小部件的内容:
小部件内容需要实时更新,因此我们需要把更新的内容放置在定时器中:
小部件内容需要实时更新,因此我们需要把更新的内容放置在定时器中:
@Override
public void onCreate() {
startTimer();
super.onCreate();
}
//开启定时器,并更新UI
private void startTimer() {
mTimer = new Timer();
mTimer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
//更新UI的方法
appUpdateUI();
}
},0,5000);
}
private void appUpdateUI() {
//获取到 AppWidgetManager 的管理器
AppWidgetManager aMW=AppWidgetManager.getInstance(this);
//远程控制View的方法,自己定义的小部件的样式。
RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.my_appwidget);
//获取进程数的方法自定义,请不要完全抄写
int processCount = ProcessInfoProvider.getProcessCount(this);
remoteViews.setTextViewText(R.id.process_count,"进程数"+processCount );
//获取剩余空间的方法,自定义,请不要完全抄写
remoteViews.setTextViewText(R.id.process_memory,"剩余空间"+ Formatter.formatFileSize(this,ProcessInfoProvider.getAvailSpace(this)));
Intent intent=new Intent("android.intent.action.Home");
intent.addCategory("android.intent.category.DEFAULT");
PendingIntent pendingIntent=PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_CANCEL_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.rr_root,pendingIntent);
PendingIntent myPendingIntent=PendingIntent.getBroadcast(this,0,new Intent("android.intent.action.KILL_BBRODE_PROCESS"),PendingIntent.FLAG_CANCEL_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.btn_clear,myPendingIntent);
ComponentName componentName = new ComponentName(this,MyAppWidgetProvider.class);
//更新小部件的内容
aMW.updateAppWidget(componentName,remoteViews);
}
6. 在清单文件中声明:
<receiver android:name=".receiver.MyAppWidgetProvider" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data android:name="android.appwidget.provider"
android:resource="@xml/my_appwidget_info" />
</receiver>r
这样一个小部件的制作就完成了