android随笔15——注册锁屏广播以及…

来源:互联网 发布:java枚举的声明 编辑:程序博客网 时间:2024/06/08 06:39

 * 锁屏时,清理后台进程
 * @author Administrator

public class LockClearService extends Service {

@Override
public IBinder onBind(Intent intent) {
return null;
}
private class LockScreenReceiver extendsBroadcastReceiver{

@Override
public void onReceive(Context context, Intent intent) {
// 清理后台进程
ActivityManager am = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
List runningAppProcesses = am.getRunningAppProcesses();
for (RunningAppProcessInfo runInfo : runningAppProcesses){
// 将 processName 做为包名来用,尝试杀死后台进程
am.killBackgroundProcesses(runInfo.processName);
}
}
}                                                                                                                                                   privateLockScreenReceiver lockScreenReceiver;                                           
@Override
public void onCreate() {
super.onCreate();
// 注册 锁屏广播 ,注意:锁屏广播只能在代码中注册 ,不能在清单文件中注册广播
lockScreenReceiver = new LockScreenReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_SCREEN_OFF);
registerReceiver(lockScreenReceiver, filter);
// 创建一个nofitycation 通知 
//参数一: 设置图标
//参数二: 设置消息
//参数三: 延迟生效时间 0立即生效
Notification notification = newNotification(R.drawable.ic_launcher, "从此以后,哥也是杀不死的进程了", 0);
// 启动activity的intent 
Intent intent = new Intent();
intent.setAction("zz.itcast.mobiolguardz10.install.shortcur.lalala");
intent.addCategory(Intent.CATEGORY_DEFAULT);
// PendingIntent 是对一个intent 和这个intent 要干的事进行封装
PendingIntent contentIntent = PendingIntent.getActivity(this,90, intent, PendingIntent.FLAG_UPDATE_CURRENT);
// 为notify添加点击时的响应事件
notification.setLatestEventInfo(this, "传智手机卫士","传智手机卫士时刻保护你的手机安全", contentIntent);
//提升安全级别为杀不死
startForeground(9898, notification);// 将当前服务提升为前台服务
}
@Override
public void onDestroy() {
super.onDestroy();
//  取消注册广播
unregisterReceiver(lockScreenReceiver);
}
0 0