4.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. 使用这个类的具体源代码 final ActivityManager am = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE); am.restartPackage(getPackageName()); 不要忘记了在配置文件中设置权限: < 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); manager.killBackgroundProcesses(getPackageName()); //需要在xml中加入权限声明 < uses-permission android:name ="android.permission.KILL_BACKGROUND_PROCESSES" /> 另外,在android2.2以后,如果服务在ondestroy里加上了start自己,用kill backgroudprocess通常无法结束自己。 还有一种最新发现的方法,利用反射调用forceStopPackage来结束进程 Method forceStopPackage = am.getClass().getDeclaredMethod("forceStopPackage", String. class); forceStopPackage.setAccessible( true);
forceStopPackage.invoke(am, yourpkgname);
配置文件中需要添加定义:
android:sharedUserId="android.uid.system" 另外需要再在配置文件添加权限:
< uses-permission android:name ="android.permission.FORCE_STOP_PACKAGES" ></ uses-permission > 并且采用系统platform签名 因为需要用FORCE_STOP_PACKAGES权限,该权限只赋予系统签名级程序 即可实现强制停止指定程序 还有一种方法 利用linux的kill -9命令 完毕。^_^
0人 了这篇文章
类别: Android┆阅读( 0)┆评论( 0) ┆ 返回博主首页┆ 返回博客首页 上一篇 Android进程与线程基本知识三 下一篇 ListView 滚动翻页/滚动加载(类似QQ空间滚动..

更多相关文章

  1. cocos2dx 移植android 报错
  2. FregServer进程,返回BR_REPLY
  3. Android使用AttributeSet自定义控件的方法
  4. Android底部导航栏实现(四)之TabLayout+ViewPager
  5. 让Android不播放关机动画,而是显示一个关机进度条的方法
  6. Android(安卓)图片加载库Glide
  7. android开机启动流程说明
  8. android下拉菜单spinner的使用方法
  9. android属性

随机推荐

  1. 【博客大赛】浅析go切片与排序
  2. 【OCP最新题库解析(052)--题2】...is tru
  3. 后端服务太多,且涉及多种语言,如何进行高效
  4. ***技能:快速提取Windows密码和Wi-Fi密钥
  5. Netty源码剖析与实战
  6. OpenCV三大经典项目实战 一次性掌握计算
  7. 在网络安全方面,苹果公司懈怠了吗?
  8. 输入年(包含闰年)和月份,输出天数
  9. 杀软对抗:一个面向***工程师的杀软绕过思
  10. 打造一款 刷Java 知识的小程序(一)