Android学习 - 如何结束进程
2015-06-09 16:03
417 查看
Android结束进程,关闭程序的方法。经过这几天的调研,发现了Android结束一个进程的方法。即采用下面这个类:
Since: API Level 3
Have the system perform a force stop ofeverything associated with the given application package. All processes thatshare its uid will be killed, all services it has running stopped, allactivities removed, etc. In addition, a ACTION_PACKAGE_RESTARTED broadcast
willbe sent, so that any of its registered alarms can be stopped, notificationsremoved, etc.
You must hold the permissionRESTART_PACKAGES to be able to call this method.
Parameters
packageName:The name of the package to be stopped.
使用这个类的具体源代码:
再加上uses-permission
结束进程还有android.os.Process.killProcess(pid)只能终止本程序的进程,无法终止其它的。
Kill the process with the given PID.Note that, though this API allows us to request to kill any process based onits PID, the kernel will still impose standard restrictions on which PIDs youare actually able to kill. Typically this means only the process
running thecaller'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.
Call this when your activity is done andshould be closed. The ActivityResult is propagated back to whoever launched youvia onActivityResult().
这是结束当前activity的方法。
在android2.2版本之后则不能再使用restartPackage()方法,而应该使用:
加入权限
另外,在android2.2以后,如果服务在ondestroy里加上了start自己,用killbackgroudprocess通常无法结束自己。
还有一种最新发现的方法,利用反射调用forceStopPackage来结束进程
需要在manifest里加上shareduid定义
另外加上权限
并且采用系统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 ofeverything associated with the given application package. All processes thatshare its uid will be killed, all services it has running stopped, allactivities removed, etc. In addition, a ACTION_PACKAGE_RESTARTED broadcast
willbe sent, so that any of its registered alarms can be stopped, notificationsremoved, etc.
You must hold the permissionRESTART_PACKAGES to be able to call this method.
Parameters
packageName:The name of the package to be stopped.
使用这个类的具体源代码:
final ActivityManager am = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE); am.restartPackage(getPackageName());
再加上uses-permission
<uses-permission android:name="android.permission.RESTART_PACKAGES" />
结束进程还有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 onits PID, the kernel will still impose standard restrictions on which PIDs youare actually able to kill. Typically this means only the process
running thecaller'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 andshould be closed. The ActivityResult is propagated back to whoever launched youvia onActivityResult().
这是结束当前activity的方法。
在android2.2版本之后则不能再使用restartPackage()方法,而应该使用:
ActivityManager manager = (ActivityManager)getSystemService(ACTIVITY_SERVICE); manager.killBackgroundProcesses(getPackageName());
加入权限
<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES"/>
另外,在android2.2以后,如果服务在ondestroy里加上了start自己,用killbackgroudprocess通常无法结束自己。
还有一种最新发现的方法,利用反射调用forceStopPackage来结束进程
Method forceStopPackage = am.getClass().getDeclaredMethod("forceStopPackage", String.class); forceStopPackage.setAccessible(true); forceStopPackage.invoke(am, yourpkgname);
需要在manifest里加上shareduid定义
android:sharedUserId="android.uid.system"
另外加上权限
<uses-permission android:name="android.permission.FORCE_STOP_PACKAGES"/>
并且采用系统platform签名
因为需要用FORCE_STOP_PACKAGES权限,该权限只赋予系统签名级程序。即可实现强制停止指定程序。
还有一种方法,利用linux的kill -9命令。
相关文章推荐
- 做Android设备的4条道路,为什么都不适合微软?!
- Android学习 1 -构建运行环境
- Android应用间跳转
- ListView setSelection 无效问题
- Android 蓝牙移植笔记
- Android 开发环境下载地址 -- 百度网盘 adt-bundle android-studio sdk adt 下载
- android学习资源
- Android调用font-awesome字体图标
- Android数据存储——SQLite
- android 之 未知错误 处理问题
- android-进阶(2)-JSON的一些理解
- Android走向闭源,化外之地中国何去何从?
- android寻找最适合资源过程
- Android 动态库死机调试方法
- android开发连接wifi addNetwork 返回-1
- Android监听短信到来并自动填充到输入框中
- 基于netty 的android Socket 聊天室客户端
- android 退出程序
- Android数据存储——SharedPreferences
- Android Ant 批量多渠道打包实例