--------------------------------------------------------------
1. 在widget类中, 在onEnabled()方法中开启widget跟新的服务;在onDisabled() 方法中停止服务
3. 当点击widget中的按钮的时候发送一条自定义广播
public
class
MyWidget
extends
AppWidgetProvider {
private
Intent
intent
;
/**
* 当widget创建的时候
*/
@Override
public
void
onEnabled(Context context) {
intent
=
new
Intent(context,UpdateWidgetService.
class
);
//开启更新widget的服务
context.startService(
intent
);
}
/**
* 当widget销毁的时候
*/
@Override
public
void
onDisabled(Context context) {
//关闭更新widget的服务
if
(
intent
!=
null
){
context.stopService(
intent
);
}
}
/**
* 系统半个小时更新次widget
*/
@Override
public
void
onUpdate(Context context, AppWidgetManager appWidgetManager,
int
[] appWidgetIds) {
super
.onUpdate(context, appWidgetManager, appWidgetIds);
intent
=
new
Intent(context,UpdateWidgetService.
class
);
//开启更新widget的服务
context.startService(
intent
);
}
}
------------------------------------------------------------------------------
2. 创建跟新widget的服务, 在服务内创建计时器, 每个一段事件, 利用widget管理器更新app的widget
public
class
UpdateWidgetService
extends
Service {
private
static
final
String
TAG
=
"UpdateWidgetService"
;
private
Timer
timer
;
//计时器
private
TimerTask
timerTask
;
private
AppWidgetManager
awm
;
@Override
public
IBinder onBind(Intent arg0) {
return
null
;
}
/**
* 服务创建时
*/
@Override
public
void
onCreate() {
super
.onCreate();
Log. i(
TAG
,
"服务开启"
);
awm
=AppWidgetManager. getInstance(getApplicationContext());
timer
=
new
Timer();
timerTask
=
new
TimerTask() {
@Override
public
void
run() {
ComponentName widget=
new
ComponentName(getApplicationContext(), MyWidget.
class
);
RemoteViews views=
new
RemoteViews(getPackageName(), R.layout.
process_widget
);
views.setTextViewText(R.id.
process_count
,
"当前进程"
+TaskMemoreryUtis.getRuingProcessCount(getApplicationContext())+
"个"
);
views.setTextViewText(R.id.
process_memory
,
"当前可用内存:"
+Formatter.formatFileSize(getApplicationContext(),
TaskMemoreryUtis. getAvailMem(getApplicationContext())));
Intent intent=
new
Intent();
intent.setAction(
"123.456"
);
PendingIntent pendingIntent=PendingIntent.getBroadcast(getApplicationContext(), 0,intent,
PendingIntent.
FLAG_UPDATE_CURRENT
);
views.setOnClickPendingIntent(R.id.
btn_clear
, pendingIntent);
//更新widget中数据 1.要更新的widget 2.widget显示的view对象
awm
.updateAppWidget(widget, views);
}
};
//计时器从widget创建1秒钟开始工作, 每隔2秒钟, 跟新widget中的内容
timer
.schedule(
timerTask
, 1000, 2000);
}
/**
* 当服务关闭时
*/
@Override
public
void
onDestroy() {
super
.onDestroy();
Log. i(
TAG
,
"服务关闭"
);
//关闭计时器
timer
.cancel();
timerTask
.cancel();
timer
=
null
;
timerTask
=
null
;
}
}
---------------------------------------------------------------------------------
4. 创建一个自定义广播类, 接收自定义广播, 在类中响应广播事件
public
class
WigitKillTaskReceiver
extends
BroadcastReceiver {
private
static
final
String
TAG
=
"WigitKillTaskReceiver"
;
@Override
public
void
onReceive(Context context, Intent intent) {
Log. i(
TAG
,
"杀死后台全部进程"
);
ActivityManager am=(ActivityManager) context.getSystemService(context .
ACTIVITY_SERVICE
);
List<RunningAppProcessInfo> runningAppProcesses = am.getRunningAppProcesses();
for
(RunningAppProcessInfo runningAppProcessInfo : runningAppProcesses) {
if
(runningAppProcessInfo.
processName
.equals( context.getPackageManager())){
continue
;
}
am.killBackgroundProcesses(runningAppProcessInfo.
processName
);
}
Toast. makeText(context ,
"清理完毕"
,0).show();
}
}