android 杀死进程的方法总结
2013-09-09 14:06
363 查看
第一种: 只能杀死当天进程
int mid=android.os.Process.myPid();
android.os.Process.killProcess(mid);
<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES"/>
第二种:可以杀死任意
ActivityManager om=(ActivityManager)getSystemService(ACTIVITY_SERVICE);
om.killBackgroundProcesses(packageName);
<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES"/>
第三种: 只能杀死当前进程
final ActivityManager am = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
am.restartPackage(getPackageName());
<uses-permission android:name="android.permission.RESTART_PACKAGES"></uses-permission>
第四种:
android2.2以后,如果服务在ondestroy里加上了start自己,用kill backgroudprocess通常无法结束自己。
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"></uses-permission>
并且采用系统platform签名
因为需要用FORCE_STOP_PACKAGES权限,该权限只赋予系统签名级程序
第五种:退出应用
Intent startMain = new Intent(Intent.ACTION_MAIN); //指定跳到系统桌面
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); //清除上一步缓存
startActivity(startMain); //开始跳转
System.exit(0);
int mid=android.os.Process.myPid();
android.os.Process.killProcess(mid);
<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES"/>
第二种:可以杀死任意
ActivityManager om=(ActivityManager)getSystemService(ACTIVITY_SERVICE);
om.killBackgroundProcesses(packageName);
<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES"/>
第三种: 只能杀死当前进程
final ActivityManager am = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
am.restartPackage(getPackageName());
<uses-permission android:name="android.permission.RESTART_PACKAGES"></uses-permission>
第四种:
android2.2以后,如果服务在ondestroy里加上了start自己,用kill backgroudprocess通常无法结束自己。
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"></uses-permission>
并且采用系统platform签名
因为需要用FORCE_STOP_PACKAGES权限,该权限只赋予系统签名级程序
第五种:退出应用
Intent startMain = new Intent(Intent.ACTION_MAIN); //指定跳到系统桌面
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); //清除上一步缓存
startActivity(startMain); //开始跳转
System.exit(0);
相关文章推荐
- Android Service保活方法总结(不被杀死)双进程守护
- android 杀死进程的方法尝试
- android杀死进程的新方法
- Android基础【1】杀死进程(强行停止)应用程序的方法
- [Android Pro] android 杀死进程的方法
- [Android Pro] android 杀死进程的方法
- android kill process 杀死进程的方法
- [Android Pro] android 杀死进程的方法
- 杀死android的进程几种方法
- Android中杀死进程的方法
- Android基础总结——进程优先级及提高优先级的方法(Service尽量不死之法)
- android 杀死进程的三种方法
- Android基础总结——进程优先级及提高优先级的方法(Service尽量不死之法)
- android结束进程方法总结
- android中杀死进程的方法
- Android结束程序杀死进程的方法
- Android 中杀死进程的三种方法汇总
- Android基础总结——进程优先级及提高优先级的方法(Service尽量不死之法)
- android 结束进程方法总结
- android中杀死进程的方法