注:锁屏广播时间是一个特殊的广播时间,在清单文件里配置广播接收者,将不会生效
只能用代码注册方式才能生效
package com.zaizai.safty.service;
import android.app.ActivityManager;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;
import java.util.List;
/**
* Created by zaizai on 2015/11/16.
*/
public class AutoClenService extends Service {
private static final String TAG = "zaizai";
private ScreenOffReceive screenOffReceive;
private ActivityManager activityManager;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
screenOffReceive = new ScreenOffReceive();
registerReceiver(screenOffReceive, new IntentFilter(Intent.ACTION_SCREEN_OFF));
super.onCreate();
}
@Override
public void onDestroy() {
unregisterReceiver(screenOffReceive);
screenOffReceive = null;
super.onDestroy();
}
private class ScreenOffReceive extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.i(TAG, "屏幕锁屏了");
List<ActivityManager.RunningAppProcessInfo> runningAppProcessInfos = activityManager.getRunningAppProcesses();
for (ActivityManager.RunningAppProcessInfo info : runningAppProcessInfos) {
activityManager.killBackgroundProcesses(info.processName);
}
}
}
}