概述
杀进程通用三种方法
activityManager .killBackgroundProcesses();
activityManager.restartPackage(); 废弃方法,android2.2以上推荐使用上面方法;android.os.Process.killProcess(); 杀掉自己创建进程。
说是杀其实是让程序重启,能杀掉后台服务。
但是如果程序service设置成foreground ,其service 服务的importance 为130 或200,为用户可感知状态,killBackgroundProcesses就无能为力了。
权限
<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES" />
<uses-permission android:name="android.permission.RESTART_PACKAGES" />
import java.util.ArrayList;
import java.util.List;import android.app.ActivityManager;
import android.app.ActivityManager.RunningServiceInfo;
import android.app.ActivityManager.RunningTaskInfo;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.util.Log;
public class Killer {
private static String TAG="Killer";
private static String [] killed ={"alala"};
private static boolean isContains(String [] src ,String target ,boolean isEqual){
boolean isIn = false;
if(target ==null || src ==null || src.length==0)return isIn;
for(String tmp:src){
if(isEqual && tmp.equalsIgnoreCase(target)){
isIn =true ;
break;
} else if(!isEqual && target.indexOf(tmp) != -1 ){
isIn = true;
break;
}
}
return isIn;
}
/**
* 杀默认进程
*/
public static void killProcess(Context context){
killProcess(context,killed);
/**
* 根据名字杀进程
*/
public static void killProcess(Context context,String []killedList){
ActivityManager activityManger=(ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> list=activityManger.getRunningAppProcesses();
StringBuffer sb = new StringBuffer("");
String processName=null;
List<Integer> killList = new ArrayList<Integer>();
if(list!=null)
for(int i=0;i<list.size();i++)
{
ActivityManager.RunningAppProcessInfo apinfo=list.get(i);
sb.append(" pid "+apinfo.pid);
sb.append(" processName "+apinfo.processName);
sb.append(" importance "+apinfo.importance);
sb.append("nr");
processName =apinfo.processName;
if(isContains(killedList,processName,false)){
Log.d(TAG,"待杀进程 "+apinfo.pid+":"+processName);
//activityManger.killBackgroundProcesses(processName);
killList.add(processName);
}
}
Log.d(TAG,"进程"+sb.toString());
sb = new StringBuffer("");
List<ActivityManager.RunningServiceInfo> listService =activityManger.getRunningServices(50);
for(int i= 0;i<listService.size();i++){
ActivityManager.RunningServiceInfo sinfo = listService.get(i);
sb.append(" pid "+sinfo.pid);
sb.append(" process "+sinfo.process);
sb.append(" uid "+sinfo.uid);
sb.append("nr");
processName =sinfo.process;
if(isContains(killed,processName,false)){
Log.d(TAG,"待杀service "+sinfo.pid+":"+processName);
//activityManger.killBackgroundProcesses(processName);
killList.add(processName);
}
}
Log.d(TAG,"Service"+sb.toString());
for(int i=0;i<killList.size();i++){
Log.d(TAG,"killing ..."+killList.get(i));
activityManger.restartPackage(killList.get(i));
}
}
public static void killProcess2(Context context) {
// 得到非系统安装程序
List<ApplicationInfo> packages = context.getPackageManager()
.getInstalledApplications(0);
ArrayList<String> packageNames = new ArrayList<String>();
ArrayList<Integer> packagePid = new ArrayList<Integer>();
for (int i = 0; i < packages.size(); i++) {
ApplicationInfo appInfo = packages.get(i);
if ((appInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {// 非系统程序
// 剔除自身
if (!appInfo.packageName.equals(context.getPackageName())) {
packageNames.add(appInfo.packageName);
packagePid.add(appInfo.uid);
Log.i("packageName", appInfo.packageName);
}
}
}
// 得到正在运行的任务
ActivityManager manager = (ActivityManager) context
.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> list = manager.getRunningTasks(20);
for (int j = 0; j < list.size(); j++) {
RunningTaskInfo taskInfo = list.get(j);
Log.i("runningTask", taskInfo.baseActivity.getPackageName());
for (int i = 0; i < packageNames.size(); i++) {
// 找到并结束进程
if (packageNames.get(i).equals(
taskInfo.baseActivity.getPackageName())) {
manager.restartPackage(packageNames.get(i));
Log.i("杀Task UnsystemRunningTasks", packageNames.get(i));
}
}
}
List<RunningServiceInfo> serviceList = manager.getRunningServices(20);
for (int j = 0; j < serviceList.size(); j++) {
RunningServiceInfo serviceInfo = serviceList.get(j);
Log.i("runningService", serviceInfo.service.getPackageName());
for (int i = 0; i < packageNames.size(); i++) {
// 找到并结束进程
if (packageNames.get(i).equals(
serviceInfo.service.getPackageName())) {
manager.restartPackage(packageNames.get(i));
Log.i("杀service UnsystemRunningServices", packageNames.get(i));
}
}
}
}
}
最后
以上就是平常柠檬为你收集整理的任务管理--杀进程的全部内容,希望文章能够帮你解决任务管理--杀进程所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复