结束进程的方法
2012-09-04 14:07
169 查看
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代码
[java] final ActivityManager am = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
am.restartPackage(getPackageName());
final ActivityManager am = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
am.restartPackage(getPackageName());
再加上uses-permission
Xml代码
[xhtml] <uses-permission android:name="android.permission.RESTART_PACKAGES"></uses-permission>
<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());
[c-sharp] ActivityManager manager = (ActivityManager)getSystemService(ACTIVITY_SERVICE);
manager.killBackgroundProcesses(getPackageName());
ActivityManager manager = (ActivityManager)getSystemService(ACTIVITY_SERVICE);
manager.killBackgroundProcesses(getPackageName());
加入权限
[c-sharp] //需要在xml中加入权限声明
<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES"/>
//需要在xml中加入权限声明
<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES"/>
另外,在android2.2以后,如果服务在ondestroy里加上了start自己,用kill backgroudprocess通常无法结束自己。
还有一种最新发现的方法,利用反射调用forceStopPackage来结束进程
代码如下
[c-sharp] Method forceStopPackage = am.getClass().getDeclaredMethod("forceStopPackage", String.class);
forceStopPackage.setAccessible(true);
forceStopPackage.invoke(am, yourpkgname);
Method forceStopPackage = am.getClass().getDeclaredMethod("forceStopPackage", String.class);
forceStopPackage.setAccessible(true);
forceStopPackage.invoke(am, yourpkgname);
需要在manifest里加上shareduid定义
[c-sharp] android:sharedUserId="android.uid.system"
android:sharedUserId="android.uid.system"
另外加上权限
[c-sharp] <uses-permission android:name="android.permission.FORCE_STOP_PACKAGES"></uses-permission>
<uses-permission android:name="android.permission.FORCE_STOP_PACKAGES"></uses-permission>
并且采用系统platform签名
因为需要用FORCE_STOP_PACKAGES权限,该权限只赋予系统签名级程序
即可实现强制停止指定程序
还有一种方法 利用linux的kill -9命令
即采用下面这个类
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代码
[java] final ActivityManager am = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
am.restartPackage(getPackageName());
final ActivityManager am = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
am.restartPackage(getPackageName());
再加上uses-permission
Xml代码
[xhtml] <uses-permission android:name="android.permission.RESTART_PACKAGES"></uses-permission>
<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());
[c-sharp] ActivityManager manager = (ActivityManager)getSystemService(ACTIVITY_SERVICE);
manager.killBackgroundProcesses(getPackageName());
ActivityManager manager = (ActivityManager)getSystemService(ACTIVITY_SERVICE);
manager.killBackgroundProcesses(getPackageName());
加入权限
[c-sharp] //需要在xml中加入权限声明
<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES"/>
//需要在xml中加入权限声明
<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES"/>
另外,在android2.2以后,如果服务在ondestroy里加上了start自己,用kill backgroudprocess通常无法结束自己。
还有一种最新发现的方法,利用反射调用forceStopPackage来结束进程
代码如下
[c-sharp] Method forceStopPackage = am.getClass().getDeclaredMethod("forceStopPackage", String.class);
forceStopPackage.setAccessible(true);
forceStopPackage.invoke(am, yourpkgname);
Method forceStopPackage = am.getClass().getDeclaredMethod("forceStopPackage", String.class);
forceStopPackage.setAccessible(true);
forceStopPackage.invoke(am, yourpkgname);
需要在manifest里加上shareduid定义
[c-sharp] android:sharedUserId="android.uid.system"
android:sharedUserId="android.uid.system"
另外加上权限
[c-sharp] <uses-permission android:name="android.permission.FORCE_STOP_PACKAGES"></uses-permission>
<uses-permission android:name="android.permission.FORCE_STOP_PACKAGES"></uses-permission>
并且采用系统platform签名
因为需要用FORCE_STOP_PACKAGES权限,该权限只赋予系统签名级程序
即可实现强制停止指定程序
还有一种方法 利用linux的kill -9命令
相关文章推荐
- Android下结束进程的方法
- 结束Excel的进程的方法
- 在linux下python爬虫进程发生异常时自动重启直至正常结束的方法
- 关于ASP.NET中调用Excel组件不能结束进程的解决方法
- Android 结束进程的方法
- 结束进程的几种方法
- Android 结束进程的方法
- Android下结束进程的方法
- window下查看端口号被占用以及结束此进程的方法
- Linux中强制结束一个进程的终极方法和其他常用命令
- C#结束进程通用方法
- .NET(C#):谈谈各种结束进程的方法
- Windows中根据端口号或PID查看进程、结束进程方法
- Windows查看进程和结束进程的方法
- android结束进程的方法!挺管用的!
- 浅谈驱动中强制结束进程的3种方法
- Android结束进程方法
- 使用ActivityManager的forceStopPackage方法结束进程
- 讲解Oracle数据库中结束死锁进程的一般方法
- Ubuntu结束进程或者程序的方法