打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Android 结束进程的方法

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 will be sent, so that any of itsregistered 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代码 

  1. final ActivityManager am = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);    
  2. am.restartPackage(getPackageName());    
 

再加上uses-permission

 

 

Xml代码 

  1. <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, thoughthis API allows us to request to kill any process based on its PID, the kernelwill still impose standard restrictions on which PIDs you are actually able tokill. Typically this means only the process running the caller'spackages/application and any additional processes created by that app; packagessharing 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 beclosed. The ActivityResult is propagated back to whoever launched you viaonActivityResult().

这是结束当前activity的方法

 

 

在android2.2版本之后则不能再使用restartPackage()方法,而应该使用killBackgroundProcesses()方法 

manager.killBackgroundProcesses(getPackageName()); 

 

  1. ActivityManager manager = (ActivityManager)getSystemService(ACTIVITY_SERVICE);       
  2. manager.killBackgroundProcesses(getPackageName());    
 

加入权限

  1. //需要在xml中加入权限声明    
  2. <uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES"/>  
 

 


 

另外,在android2.2以后,如果服务在ondestroy里加上了start自己,用kill backgroudprocess通常无法结束自己。

 

还有一种最新发现的方法,利用反射调用forceStopPackage来结束进程

 

代码如下

 

  1. Method forceStopPackage = am.getClass().getDeclaredMethod("forceStopPackage", String.class);  
  2. forceStopPackage.setAccessible(true);  
  3. forceStopPackage.invoke(am, yourpkgname);  
 

 

需要在manifest里加上shareduid定义

  1. android:sharedUserId="android.uid.system"  
 

 

另外加上权限

  1. <uses-permission android:name="android.permission.FORCE_STOP_PACKAGES"></uses-permission>  
  2.    
 

 

并且采用系统platform签名


因为需要用FORCE_STOP_PACKAGES权限,该权限只赋予系统签名级程序

 

即可实现强制停止指定程序

 

还有一种方法 利用linux的kill -9命令

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
利用反射调用forceStopPackage来结束进程
Android SDK1.0 Permission 大全访问权限许可
炼妖壶(Island)(com.oasisfeng.island)
Android权限大全
Android apk的安装、卸载、更新升级(通过Eclipse实现静默安装)
android开发:Manifest.permission常用的权限
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服