以下内容大部分来自api文档
一,android中杀死进程的方法
1,android.os.Process中的 public static final void killProcess(int pid)
Kill the process with the given PID. Note that, though this API allows us to request to kill any process based on its PID, the kernel will still impose standard restrictions on which PIDs you are actually able to kill. Typically this means only the process running the caller's packages/application and any additional processes created by that app; packages sharing a common UID will also be able to kill each other's processes.
上面介绍的大概意思就是:使用方法1只能写在该程序中杀死自己,不能杀死别人
2,ActivityManager中的 public void killBackgroundProcess(String packageName)
Have the system immediately kill all background processes associated with the given package. This is the same as the kernel killing those processes to reclaim memory; the system will take care of restarting these processes in the future as needed.
You must hold the permission KILL_BACKGROUND_PROCESSES
to be able to call this method.
如果系统需要,他会在未来的某个时候重新启动
3,AcitivityManager中的public void restartPackage(String packageName)
This method was deprecated in API level 8.
This is now just a wrapper for killBackgroundProcesses(String)
; the previous behavior here is no longer available to applications because it allows them to break other applications by removing their alarms, stopping their services, etc.
这个API已经在API level 8的时候废弃掉了
因为他已经不再适用于应用程序了,因为他会破坏其他应用通过移除他们的闹钟,停止他们的服务等等
4,这个api是系统隐藏的api ,ActivityManager的public void forceStopPackage(String packageName)
Have the system perform a force stop of everything associated with the given application package. All processes that share its uid
will be killed, all services it has running stopped, all activities removed, etc. In addition, a {@link Intent#ACTION_PACKAGE_RESTARTED}
broadcast will be sent, so that any of its registered alarms can be stopped, notifications removed, etc.
You must hold the permission FORCE_STOP_PACKAGES
This is not available to third party applications due to
it allowing them to break other applications by stopping their
services, removing their alarms, etc.
上面的意思是:可以强制停止任何和这个应用包名相关的进程.所有共享uid的进程也会被杀死,所有正在运行的服务也会停止,所有activity会被移除
除此之外,当使用该方法是一个动作ACTION_PACKAGE_RESTARTED的广播会被发送,用来任何注册闹钟的进程停止,通知移除等
当使用该方法的时候必须需要权限FORCE_STOP_PACKAGES
这个API在API level 8 的时候被废除,废除的原因是,他不适用于第三方应用,因为他允许他们破坏其他应用通过停止他们的服务,移除他们的闹钟等
---------------------------------------------------------
虽然该api被隐藏了,但是要是使用也不是不可以,需要如下的步骤
A,清单文件声明共享系统uid
B,加入权限FORCE_STOP_PACKAGES
C,添加系统签名<这个可能会比较麻烦>
D,使用反射的手段获取API
二,一些知名应用的清除内存,杀死后台进程的方法
对百度桌面和360桌面进行了反编译,查看其清单配置文件,发现清单配置文件中对杀死进程只使用了两个杀死进程的权限
KILL_BACKGROUND_PROCESSES
和FORCE_STOP_PACKAGES,由此可以大致推测他们杀死进程是使用ActivityManager中的killBackgroundProcess(String packageName)和restartPackage(String packageName).使用这两个桌面的清除后台进程的功能,发现一些进程被清除过后,2-3秒后会自动重新启动
三,对killBackgroundProcess(String packageName)和restartPackage(String packageName)杀死进程方法自己的尝试
A,使用killBackgroundProcess(String packageName)杀死进程的时候发现该方法执行过后后台进程确实被杀死了,但是2-3秒后会自动重启
B,使用restartPackage(String packageName)杀死进程的时候发现该方法执行过后后台进程别杀死,但是会有10秒靠上的"正在重启..."的提示,然后过一段时间后台进程会自动复活