Android 結束進程的方法

來源:互聯網
上載者:User


Android 結束進程,關閉程式的方法,經過這幾天的調研,發現了Android結束一個進程的方法

即採用下面這個類

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

packageName

The name of the package to be stopped.

 

 

使用這個類的具體原始碼

Java代碼 

final ActivityManager am = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);<br />am.restartPackage(getPackageName());  

再加上uses-permission

 

 

Xml代碼 

<uses-permission android:name="android.permission.RESTART_PACKAGES"></uses-permission>  

 

 

結束進程還有android.os.Process.killProcess(pid)只能終止本程式的進程,無法終止其它的

 

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().

這是結束當前activity的方法

 

 

在android2.2版本之後則不能再使用restartPackage()方法,而應該使用killBackgroundProcesses()方法 

manager.killBackgroundProcesses(getPackageName()); 

 

ActivityManager manager = (ActivityManager)getSystemService(ACTIVITY_SERVICE);<br />manager.killBackgroundProcesses(getPackageName());  

加入許可權

//需要在xml中加入許可權聲明<br /><uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES"/> 

 


 

另外,在android2.2以後,如果服務在ondestroy裡加上了start自己,用kill backgroudprocess通常無法結束自己。

 

還有一種最新發現的方法,利用反射調用forceStopPackage來結束進程

 

代碼如下

 

Method forceStopPackage = am.getClass().getDeclaredMethod("forceStopPackage", String.class);<br />forceStopPackage.setAccessible(true);<br />forceStopPackage.invoke(am, yourpkgname); 

 

需要在manifest裡加上shareduid定義

android:sharedUserId="android.uid.system" 

 

另外加上許可權

<uses-permission android:name="android.permission.FORCE_STOP_PACKAGES"></uses-permission><br />  

 

並且採用系統platform簽名


因為需要用FORCE_STOP_PACKAGES許可權,該許可權只賦予系統簽名級程式

 

即可實現強制停止指定程式

 

還有一種方法 利用linux的kill -9命令

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.