<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/appwidget_info"/>
</receiver>
public class MyAppWidgetProvider extends AppWidgetProvider {
//下面每个方法被调用前都会调用此方法
@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
}
//第一个widget创建
@Override
public void onEnabled(Context context) {
/*开启相同的服务,只调用onCreate方法一次,在onCreate方法里进行监听widget的ui更新。
避免进行多次监听,用计时器和RemoteView,AppWidgetManager进行监听。在下面函数都开启服务。这样就避免多次避免服务被杀死,
*/
context.startService(new Intent(context, UpdateWidgetService.class));
super.onEnabled(context);
}
//widget个数添加
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
context.startService(new Intent(context, UpdateWidgetService.class));
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
//widget配置改变时,widget宽高或者widget个数增加
@Override
public void onAppWidgetOptionsChanged(Context context, AppWidgetManager appWidgetManager, int appWidgetId, Bundle newOptions) {
context.startService(new Intent(context, UpdateWidgetService.class));
super.onAppWidgetOptionsChanged(context, appWidgetManager, appWidgetId, newOptions);
}
//最后一个widget关闭
@Override
public void onDisabled(Context context) {
context.stopService(new Intent(context, UpdateWidgetService.class));
super.onDisabled(context);
}
//关闭一个widget
@Override
public void onDeleted(Context context, int[] appWidgetIds) {
super.onDeleted(context, appWidgetIds);
}
}
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth = "294dp"
android:minHeight = "72dp"
android:initialLayout ="@layout/appwidget"/>
public class UpdateWidgetService extends Service {
private Context mContext;
private Timer timer;
private InnerReceiver innerReceiver;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
mContext=this;
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
intentFilter.addAction(Intent.ACTION_SCREEN_ON);
innerReceiver = new InnerReceiver();
registerReceiver(innerReceiver,intentFilter);
startTimer();
super.onCreate();
}
class InnerReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(Intent.ACTION_SCREEN_ON))
{
startTimer();
}
else
{
if(timer!=null) {
timer.cancel();
timer = null;
}
}
}
}
private void startTimer() {
//定时器更新
timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
//获取AppWidgetManager对象
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(mContext);
//获得远程View。窗体小控件的View
RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.appwidget);
Intent intent = new Intent("android.intent.action.HOME");
intent.addCategory("android.intent.category.DEFAULT");
PendingIntent pendingIntent = PendingIntent.getActivity(mContext,0,intent,PendingIntent.FLAG_CANCEL_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.app_linear,pendingIntent);
//设置按钮点击事件,杀进程通过广播接收者来处理
Intent intent1 = new Intent("android.intent.action.KILL_BACKGROUD_PROCESS");
intent1.addCategory("android.intent.category.DEFAULT");
PendingIntent pendingIntent1 = PendingIntent.getActivity(mContext,0,intent1,PendingIntent.FLAG_CANCEL_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.appbtn_clear,pendingIntent1);
//设置对应的View内容
remoteViews.setTextViewText(R.id.process_count,"进程总数:"+ ProgressInfoUtil.getProgressCount(mContext));
remoteViews.setTextViewText(R.id.process_memory,"可用内存:"+ProgressInfoUtil.getAvailSpace(mContext));
//窗体小部件对应广播接收者的字节码文件
ComponentName componentName = new ComponentName(mContext, MyAppWidgetProvider.class);
//更新窗体小部件
appWidgetManager.updateAppWidget(componentName,remoteViews);
}
},0,5000);
}
@Override
public void onDestroy() {
if(innerReceiver!=null)
unregisterReceiver(innerReceiver);
timer.cancel();
timer=null;
super.onDestroy();
}
注:更新频率必须大于半小时。
联系客服