private ActivityManager manager;
private ListrunningProcesses;
private String packName;
private PackageManager pManager;
@Override
public void onReceive(Context context, Intent intent) {
pManager = context.getPackageManager();
manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
killOthers(context);
}
/**
* 杀死其他正在运行的程序
*
* @param context
*/
private void killOthers(Context context) {
runningProcesses = manager.getRunningAppProcesses();
for (RunningAppProcessInfo runningProcess : runningProcesses) {
try {
packName = runningProcess.processName;
ApplicationInfo applicationInfo = pManager.getPackageInfo(packName, 0).applicationInfo;
if (!"com.asms".equals(packName)&&filterApp(applicationInfo)) {
forceStopPackage(packName,context);
System.out.println(packName+"JJJJJJ");
}
}catch(Exception e){
e.printStackTrace();
}
}
}
/**
*强制停止应用程序
* @param pkgName
*/
private void forceStopPackage(String pkgName,Context context) throws Exception{
ActivityManager am = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
Method method = Class.forName("android.app.ActivityManager").getMethod("forceStopPackage", String.class);
method.invoke(am, pkgName);
}
/**
* 判断某个应用程序是 不是三方的应用程序
* @param info
* @return
*/
public boolean filterApp(ApplicationInfo info) {
if ((info.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0) {
return true;
} else if ((info.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
return true;
}
return false;
}