---------清单文件中--------
<!--widget的注册-->
<receiver android:name=".widget.MyWidget">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data android:name="android.appwidget.provider"
android:resource="@xml/widgetinfo" /> <!--这是widget的原始数据-->
</receiver>
<service android:name="com.lambo.servicer.UpdateWidgetServicer"/>
----------res/xml/widgetinfo.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="8640000"
android:previewImage="@drawable/shapenomal"
android:initialLayout="@layout/widgetlayout"
android:resizeMode="horizontal|vertical">
</appwidget-provider>
-----------res/layout/widgetlayout.xml-------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:id="@+id/tv_vidget"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="widget"/>
<Button
android:id="@+id/btn_yijianclear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="一键清理"/>
</LinearLayout>
----------UpdateWidgetServicer--------
public class UpdateWidgetServicer extends Service {
private Timer timer;
private TimerTask task;
AppWidgetManager awm;
private ScreenOffReceiver offReceiver;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
//注册屏幕锁屏的广播接受者
offReceiver = new ScreenOffReceiver();
registerReceiver(offReceiver,new IntentFilter(Intent.ACTION_SCREEN_OFF));
//在这里要做服务开启的时候,注册打开屏幕和锁屏的广播接者,在广播接受者中要开启或关闭定时器----这里暂时没有做
awm = AppWidgetManager.getInstance(UpdateWidgetServicer.this);
timer=new Timer();
task=new TimerTask() {
@Override
public void run() {
ComponentName provider=new ComponentName(UpdateWidgetServicer.this, MyWidget.class);//设置更新的组件
RemoteViews views=new RemoteViews(getPackageName(), R.layout.widgetlayout);//桌面找到包下面的布局
views.setTextViewText(R.id.tv_vidget,"ceshi");
awm.updateAppWidget(provider,views);
//要自定义一个广播事件,要杀死进程
Intent intent=new Intent();
intent.setAction("killalllprogress");
PendingIntent pendintent=PendingIntent.getBroadcast(getApplicationContext(),0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
views.setOnClickPendingIntent(R.id.btn_yijianclear,pendintent);//设置widget上按钮的点击事件
}
};
timer.schedule(task,0,3000);
}
//锁屏的广播接受者
private class ScreenOffReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
//停止计时器
if(timer!=null && task!=null) {
timer.cancel();
task.cancel();
timer = null;
task = null;
}
}
}
@Override
public void onDestroy() {
////在这里要关闭打开屏幕和锁屏的广播接者,---这类暂时没有做
super.onDestroy();
offReceiver=null;
if(timer!=null && task!=null) {
timer.cancel();
task.cancel();
timer = null;
task = null;
}
}
}
------------Mywidget广播接受者--------
/**
* Created by wofu on 2018/4/27.
* 小控件广播接受者
*/
public class MyWidget extends AppWidgetProvider {
@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
//开启服务
Intent intent=new Intent(context, UpdateWidgetServicer.class);
context.startService(intent);
}
//刚开始添加widget的时候只会执行一次
@Override
public void onEnabled(Context context) {
super.onEnabled(context);
//开启服务
Intent intent=new Intent(context, UpdateWidgetServicer.class);
context.startService(intent);
}
//没有widget的时候,会执行一次
@Override
public void onDisabled(Context context) {
super.onDisabled(context);
//关闭服务
Intent intent=new Intent(context, UpdateWidgetServicer.class);
context.stopService(intent);
}
}