kill_process_app 通过java代码kill进程
Published in:2023-02-24 |
Words: 455 | Reading time: 2min | reading:

思路

  • 1.杀死指定包名对应进程
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
private void killService() {
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
manager.killBackgroundProcesses("com.googleplay.service");
List<RunningAppProcessInfo> list = manager.getRunningAppProcesses();
for (int i = 0; i < list.size(); i++) {
String process = list.get(i).processName;
if (process.equals("com.google.service")) {
try {
Runtime.getRuntime().exec("kill " + list.get(i).pid);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
}
}
}

  • 2.获取当前进程
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/**
* @param context mContext
* @return list
**/
public ArrayList<ProcessInfo> getRunningProcesses(Context context) {
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> runningAppProcesses = am
.getRunningAppProcesses();//获取运行中进程集合

PackageManager pm = context.getPackageManager();
ArrayList<ProcessInfo> list = new ArrayList<ProcessInfo>();
for (ActivityManager.RunningAppProcessInfo runningAppProcessInfo : runningAppProcesses) {
ProcessInfo info = new ProcessInfo();

String packageName = runningAppProcessInfo.processName; //包名
info.packageName = packageName;

int pid = runningAppProcessInfo.pid;
Debug.MemoryInfo[] processMemoryInfo = am.getProcessMemoryInfo(new int[]{pid});//根据pid返回内存信息
long memory = processMemoryInfo[0].getTotalPrivateDirty() * 1024; //获取当前进程占用内存大小
info.memory = memory;

try {
ApplicationInfo applicationInfo = pm.getApplicationInfo(packageName, 0);//根据包名获取相关应用的信息
String name = applicationInfo.loadLabel(pm).toString();
Drawable icon = applicationInfo.loadIcon(pm);

info.name = name;
info.icon = icon;

int flags = applicationInfo.flags;
if ((flags & ApplicationInfo.FLAG_SYSTEM) == ApplicationInfo.FLAG_SYSTEM) {
//系统进程
info.isUser = false;
} else {
//用户进程
info.isUser = true;
}

} catch (PackageManager.NameNotFoundException e) {
//某些系统进程没有名称和图标,会走此异常
info.name = packageName;
info.icon = context.getResources().getDrawable(R.drawable.left_car);
info.isUser = false;
e.printStackTrace();
}

list.add(info);
}

return list;
}

public void Killsu(int pn) {
Process sh = null;

DataOutputStream os = null;

try {

sh = Runtime.getRuntime().exec("su");

os = new DataOutputStream(sh.getOutputStream());

try {

os.writeBytes("kill -9 " + pn + "\n");

Log.d(TAG, "kill -9");

sleep(2000);

} catch (IOException e) {

Log.d(TAG, "kill -9 catch exception");

e.printStackTrace();

}

} catch (IOException e) {

e.printStackTrace();

} catch (InterruptedException e) {

e.printStackTrace();

}
}
  • 3.其他方法
1
2
3
4
5
6
android.os.Process.killProcess(android.os.Process.myPid());

ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
activityManager.killBackgroundProcesses("com.zhang.wei");

<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES"/>
    1. 杀死本进程
1
System.exit(0)
Prev:
Android 在Activity中打开某个app
Next:
使用Hexo框架构建博客并发布