Android两种杀掉进程方式总结(System.exit()和Process.killProcess())

Android两种杀掉进程方式总结(System.exit()和Process.killProcess())

1.影响范围不同

System.exit(0)只会影响当前的程序;
android.os.Process.killProcess(android.os.Process.myPid())会杀掉所有PID一样的进程,比如那些拥有相同UID的应用,统统都会被杀掉。

2. 方式不一样

System.exit(0)是停止程序的虚拟机;
android.os.Process.killProcess(android.os.Process.myPid()) 是通过PID去杀死进程。

3. log 区别

  3.1 System.exit(0)

 行 5652: 04-23 17:38:23.704     0     0 E binder_alloc: 17913: binder_alloc_buf, no vma
    行 5666: 04-23 17:38:23.739   533   652 W libprocessgroup: kill(-17913, 9) failed: No such process
    行 5667: 04-23 17:38:23.739   533   652 I libprocessgroup: Successfully killed process cgroup uid 1000 pid 17913 in 0ms
    行 5669: 04-23 17:38:23.740   533 13913 I ActivityManager: Process com.example.myapplication (pid 17913) has died: cch  CRE 
    行 5671: 04-23 17:38:23.740   533 13913 I am_proc_died: [0,17913,com.example.myapplication,900,17]
    行 5675: 04-23 17:38:23.741   385   385 I Zygote  : Process 17913 exited cleanly (0)
    行 5699: 04-23 17:38:23.817   533   706 W ActivityManager: setHasOverlayUi called on unknown pid: 17913

   3.2Process.killProcess() 

行 9300: 04-23 17:44:15.206     0     0 E binder_alloc: 20452: binder_alloc_buf, no vma
    行 9307: 04-23 17:44:15.227   533  7244 I ActivityManager: Process com.example.myapplication (pid 20452) has died: cch  CRE 
    行 9308: 04-23 17:44:15.227   533  7244 I am_proc_died: [0,20452,com.example.myapplication,900,17]
    行 9309: 04-23 17:44:15.229   533   652 W libprocessgroup: kill(-20452, 9) failed: No such process
    行 9311: 04-23 17:44:15.229   533   652 I libprocessgroup: Successfully killed process cgroup uid 1000 pid 20452 in 0ms
    行 9313: 04-23 17:44:15.237   385   385 I Zygote  : Process 20452 exited due to signal (9)
    行 9319: 04-23 17:44:15.283   533   706 W ActivityManager: setHasOverlayUi called on unknown pid: 20452

你可能感兴趣的:(android)