android 杀死后台进程,Android 杀进程相关方法

  • Post author:
  • Post category:其他


1.1 ActivityManager::forceStopPackage

/**

* Have the system perform a force stop of everything associated with

* the given application package. All processes that share its uid // 同UID

* will be killed, all services it has running stopped, all activities // service activity

* removed, etc. In addition, a {@link Intent#ACTION_PACKAGE_RESTARTED} //ACTION_PACKAGE_RESTARTED广播

* broadcast will be sent, so that any of its registered alarms can // alarm notifications

* be stopped, notifications removed, etc.

*

*

You must hold the permission

* {@link android.Manifest.permission#FORCE_STOP_PACKAGES} to be able to

* call this method.

*

* @param packageName The name of the package to be stopped.

* @param userId The user for which the running package is to be stopped.

*

* @hide 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.

*/

public void forceStopPackageAsUser(String packageName, int userId) {

try {

ActivityManagerNative.getDefault().forceStopPackage(packageName, userId);

} catch (RemoteException e) {

throw e.rethrowFromSystemServer();

}

}

调用此方法,(FORCE_STOP_PACKAGES权限需要系统签名)

系统会强制停止与指定包关联的所有事情,

将会杀死使用同一个uid的所有进程,停止所有服务,移除所有activity;

所有注册的定时器和通知也会移除;

还可以达到禁止开机自启动和后台自启动的目的;

1.2 PackageManagerService::setPackageStoppedState 设置包停止状态

/**

* Set whether the given package should be considered stopped, making

* it not visib