void android.app.ActivityManager.restartPackage(String packageName) public void restartPackage (String packageName) Since: API Level 3 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 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 RESTART_PACKAGES to be able to call this method. Parameters
使用這個(gè)類的具體源代碼 Java代碼
再加上uses-permission
Xml代碼
結(jié)束進(jìn)程還有android.os.Process.killProcess(pid)只能終止本程序的進(jìn)程,無法終止其它的
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.
public void finish ()Call this when your activity is done and should be closed. The ActivityResult is propagated back to whoever launched you via onActivityResult(). 這是結(jié)束當(dāng)前activity的方法
在android2.2版本之后則不能再使用restartPackage()方法,而應(yīng)該使用killBackgroundProcesses()方法
加入權(quán)限
另外,,在android2.2以后,,如果服務(wù)在ondestroy里加上了start自己,,用kill backgroudprocess通常無法結(jié)束自己,。
還有一種最新發(fā)現(xiàn)的方法,,利用反射調(diào)用forceStopPackage來結(jié)束進(jìn)程
代碼如下
需要在manifest里加上shareduid定義
另外加上權(quán)限
并且采用系統(tǒng)platform簽名 因?yàn)樾枰肍ORCE_STOP_PACKAGES權(quán)限,,該權(quán)限只賦予系統(tǒng)簽名級程序
即可實(shí)現(xiàn)強(qiáng)制停止指定程序
還有一種方法 利用linux的kill -9命令 |
|