http://blog.csdn.net/huxueyan521/article/details/8921976html
作一個應用,須要強制關閉進程。 java
能夠使用ActivityManager的killBackgroundProcesses方法,須要權限android.permission.KILL_BACKGROUND_PROCESSES。但使用此方法殺死進程後,進程會重啓。源碼中解釋以下: android
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. app
爲了強制關閉進程,但願使用ActivityManager的另一個方法,forceStopPackage。源碼中解釋以下: ide
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. ui
使用這個方法有兩點須要注意: this
- 此方法是@hide的方法: spa
解決方案是使用java的反射機制完成調用,代碼以下: .net
ActivityManager mActivityManager = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
Method method = Class.forName("android.app.ActivityManager").getMethod("forceStopPackage", String.class);
method.invoke(mActivityManager, packageName); //packageName是須要強制中止的應用程序包名 rest
- 此方法須要權限:android.permission.FORCE_STOP_PACKAGES
下面着手分析這個權限。
這個權限在frameworks/base/core/res/AndroidManifest.xml文件中聲明,以下:
android:permissionGroup="android.permission-group.SYSTEM_TOOLS"
android:protectionLevel="signature"
android:label="@string/permlab_forceStopPackages"
android:description="@string/permdesc_forceStopPackages"/>
注意protectionLevel屬性值未signature。看sdk文檔 http://developer.android.com/guide/topics/manifest/permission- element.html#plevel中對這一屬性的解釋以下:
A permission that the system grants only if the requesting application is signed with the same certificate as the application that declared the permission. If the certificates match, the system automatically grants the permission without notifying the user or asking for the user's explicit approval.
意思是:app使用FORCE_STOP_PACKAGES權限,app必須和這個權限的聲明者的簽名保持一致!
FORCE_STOP_PACKAGES的聲明者是frameworks/base/core/res/,能夠在frameworks/base/core/res/Android.mk中看到它的簽名信息:
LOCAL_NO_STANDARD_LIBRARIES := true
LOCAL_PACKAGE_NAME := framework-res
LOCAL_CERTIFICATE := platform
即,簽名爲platform.
最終獲得結論,app須要是platform簽名,才能夠使用forceStopPackage方法!
網上有不少文章說起,須要在app的AndroidManifest.xml中添加 android:sharedUserId="android.uid.system"一句話。看 sdk(http://developer.android.com/guide/topics/manifest/manifest- element.html)對此的解釋:
android:sharedUserId The name of a Linux user ID that will be shared with other applications. By default, Android assigns each application its own unique user ID. However, if this attribute is set to the same value for two or more applications, they will all share the same ID — provided that they are also signed by the same certificate. Application with the same user ID can access each other's data and, if desired, run in the same process.
意思是,兩個app使用了相同的user id,就能夠互相訪問對方的數據。所以,app使用android.uid.system的user id,就能夠訪問系統數據。注意背景爲黃色的一句,這裏依然須要兩個app有相同的簽名才行。