http://www.eoeandroid.com/home.php?mod=space&uid=588465&do=blog&id=2667

做android开发时,发现一个关于android退出时不能彻底关闭的问题,比如:一个程序里new 出了N多个Thread,这样在退出程序的可能不能完全关闭,最后发现,只用finish()方法,有时候不能彻底退出,个人感觉还是要在适当的地方加上:System.exit(0);

-=====-=-=-=-=-=======-----=====

1. finish()方法

该方法可以结束当前 Activity,但是如果你的App有很多 Activity 的话,使用该方法显得有点捉襟见肘了。

另外,还有一个方法finishActivity (int requestCode) ,关于这个方法,先看看sdk的api说明吧!

 
  1. public void finishActivity (int requestCode)
  2. Since: API Level 1
  3. Force finish another activity that you had previously started with startActivityForResult(Intent, int).
  4. Parameters requestCode The request code of the activity that you had given to startActivityForResult(). If there are multiple activities started with this request code, they will all be finished.

也许你会这样理解 ,Activity1 通过方法 startActivityForResult (Intent, int) 启动 Activity2,然后在 Activity2 中通过方法finishActivity (int requestCode)来结束 Activity1,但是很不幸运,不是这样的。不信你可以Demo一把!

上面文档说得很明白,该方法强制关闭通过方法 startActivityForResult (Intent, int) 启动的 Activity,也就是说在 Activity1 中的(重写)方法onActivityResult(int requestCode, int resultCode, Intent data) 来接收 Activity2 返回的结果,必须在 Activity1 中调用finishActivity (int requestCode)来结束 Activity2。(一般在onActivityResult 方法调用该方法结束 Activity2)。

 
  1. Force finish another activity that you had previously started with startActivityForResult(Intent, int).
  2. Parameters

还有,下面两个方法,可以参阅文档以及源码研究一下。

 
  1. finishActivityFromChild(Activity child, int requestCode)
  2. finishFromChild(Activity child)

2. killProcess

通过调用 android.os.Process 的相关方法,结束 App,示例如下:

 
  1. btn_exit.setOnClickListener(new Button.OnClickListener() {
  2. @Override
  3. public void onClick(View v) {
  4. android.os.Process.killProcess(android.os.Process.myPid());
  5. }
  6. });

3. exit

我们知道,Java 的 exit(int code) 方法可以退出程序,通过查看该方法源码,知道它实际上是调用下面的方法:

 
  1. Runtime.getRuntime().exit(code);

示例代码,如下所示:

  1. btn_exit.setOnClickListener(new Button.OnClickListener() {
  2. @Override
  3. public void onClick(View v) {
  4. System.exit(0);//正常退出App
  5. }
  6. });

接下来,我们研究一下这个方法。java.lang.System这个类的该方法jdk说明:

  1. exit
  2. public static void exit(int status)
  3. 终止当前正在运行的 Java 虚拟机。参数用作状态码;根据惯例,非 0 的状态码表示异常终止。
  4. 该方法调用 Runtime 类中的 exit 方法。该方法永远不会正常返回。
  5. 调用 System.exit(n) 实际上等效于调用:
  6. Runtime.getRuntime().exit(n)
  7. 参数:
  8. status - 退出状态。
  9. 抛出:
  10. SecurityException - 如果安全管理器存在并且其 checkExit 方法不允许以指定状态退出。
  11. 另请参见:
  12. Runtime.exit(int)

也就是说,参数为非0值的话是异常退出程序。参数为0的话,就是正常退出。

看RunTime这个类的该方法源码:

  1. public void exit(int status) {
  2. SecurityManager security = System.getSecurityManager();
  3. if (security != null) {
  4. security.checkExit(status);
  5. }
  6. Shutdown.exit(status);
  7. }

其api说明:

  1. exit
  2. public void exit(int status)
  3. 通过启动虚拟机的关闭序列,终止当前正在运行的 Java 虚拟机。此方法从不正常返回。可以将变量作为一个状态码;根据惯例,非零的状态码表示非正常终止。
  4. 虚拟机的关闭序列包含两个阶段。在第一个阶段中,会以某种未指定的顺序启动所有已注册的关闭钩子 (hook)(如果有的话),并且允许它们同时运行直至结束。在第二个阶段中,如果已启用退出终结,则运行所有未调用的终结方法。一旦完成这个阶段,虚拟机就会暂停。
  5. 如果在虚拟机已开始其关闭序列后才调用此方法,那么若正在运行关闭钩子,则将无限期地阻断此方法。如果已经运行完关闭钩子,并且已启用退出终结 (on-exit finalization),那么此方法将利用给定的状态码(如果状态码是非零值)暂停虚拟机;否则将无限期地阻断虚拟机。
  6. System.exit 方法是调用此方法的一种传统而便捷的方式。
  7. 参数:
  8. status - 终止状态。按照惯例,非零的状态码表明非正常终止。
  9. 抛出:
  10. SecurityException - 如果安全管理器存在,并且其 checkExit 方法不允许存在指定的状态
  11. 另请参见:
  12. SecurityException, SecurityManager.checkExit(int), addShutdownHook(java.lang.Thread), removeShutdownHook(java.lang.Thread), runFinalizersOnExit(boolean), halt(int)

该方法又是调用Shutdown这个类的exit()方法。

  1. static void exit(int status) {
  2. boolean runMoreFinalizers = false;
  3. synchronized (lock) {
  4. if (status != 0) runFinalizersOnExit = false;
  5. switch (state) {
  6. case RUNNING: /* Initiate shutdown */
  7. state = HOOKS;
  8. break;
  9. case HOOKS: /* Stall and halt */
  10. break;
  11. case FINALIZERS:
  12. if (status != 0) {
  13. /* Halt immediately on nonzero status */
  14. halt(status);
  15. else {
  16. /* Compatibility with old behavior:
  17. * Run more finalizers and then halt
  18. */
  19. runMoreFinalizers = runFinalizersOnExit;
  20. }
  21. break;
  22. }
  23. }
  24. if (runMoreFinalizers) {
  25. runAllFinalizers();
  26. halt(status);
  27. }
  28. synchronized (Shutdown.class) {
  29. /* Synchronize on the class object, causing any other thread
  30. * that attempts to initiate shutdown to stall indefinitely
  31. */
  32. sequence();
  33. halt(status);
  34. }
  35. }

其中,runAllFinalizers()是一个本地方法:

  1. JNIEXPORT void JNICALL
  2. Java_java_lang_Shutdown_runAllFinalizers(JNIEnv *env, jclass ignored)
  3. {
  4. jclass cl;
  5. jmethodID mid;
  6. if ((cl = (*env)->FindClass(env, "java/lang/ref/Finalizer"))
  7. && (mid = (*env)->GetStaticMethodID(env, cl,
  8. "runAllFinalizers""()V"))) {
  9. (*env)->CallStaticVoidMethod(env, cl, mid);
  10. }
  11. }

System.exit()的参数是把退出原因返回给系统, 一般来说可以是任何的整数 。

0表示正常退出,1表示非正常 。

最后说一下finish()与exit方法的区别:

finish()是Activity的类方法,仅仅针对Activity,当调用finish()时,只是将活动推向后台,并没有立即释放内存,活动的资源并没有被清理;当调用System.exit(0)时,退出当前Activity并释放资源(内存),但是该方法不可以结束整个App如有多个Activty或者有其他组件service等不会结束。

其实android的机制决定了用户无法完全退出应用,当你的application最长时间没有被用过的时候,android自身会决定将application关闭了。


4. restartPackage方法

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

首先需要创建ActivityManager对象,然后调用restartPackage()方法(如果有兴趣的话,可以看源码)。

注意:getPackageName获得当前应用包名称,如mark.zhang

使用这种方式来退出App,需要权限:

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

更加详细的说明,如下:

  1. void android.app.ActivityManager.restartPackage(String packageName)
  2. 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 Intent.ACTION_PACKAGE_RESTARTED broadcast will be sent, so that any of its registered alarms can be stopped, notifications removed, etc.
  3. You must hold the permission android.Manifest.permission.RESTART_PACKAGES to be able to call this method.
  4. Parameters:
  5. packageName The name of the package to be stopped.

可以看出,相同的UID的进程会被kill,还会停止相关的服务以及移除所有的Activity,并且会发送一个广播。


注意一个问题:在android2.2之后,该方法不可以将应用程序结束,需要使用ActivityManager类的下面这个方法:

  1. public void killBackgroundProcesses (String packageName)

api 文档说的很清楚:

  1. public void restartPackage (String packageName)
  2. Since: API Level 3
  3. This method is deprecated.
  4. This is now just a wrapper for killBackgroundProcesses(String); the previous behavior here is no longer available to applications because it allows them to break other applications by removing their alarms, stopping their services, etc.

另外,需要使用权限:

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

但是不管你怎么样折腾,还是无法退出App,呜呼哀哉!这里给出一个方法:

  1. int currentVersion = android.os.Build.VERSION.SDK_INT;
  2. if (currentVersion > android.os.Build.VERSION_CODES.ECLAIR_MR1) {
  3. Intent startMain = new Intent(Intent.ACTION_MAIN);
  4. startMain.addCategory(Intent.CATEGORY_HOME);
  5. startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  6. startActivity(startMain);
  7. System.exit(0);
  8. else {// android2.1
  9. ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
  10. am.restartPackage(getPackageName());
  11. }


关于android.os.Build.VERSION.SDK_INT,可以参考 http://blog.csdn.net/androidbluetooth/article/details/6778422


更多相关文章

  1. 浅谈Java中Collections.sort对List排序的两种方法
  2. Python list sort方法的具体使用
  3. python list.sort()根据多个关键字排序的方法实现
  4. android上一些方法的区别和用法的注意事项
  5. Android(安卓)Activity界面切换添加动画特效
  6. android实现字体闪烁动画的方法
  7. Android(安卓)Wifi模块分析(三)
  8. Android中dispatchDraw分析
  9. Android四大基本组件介绍与生命周期

随机推荐

  1. Android应用程序组件Content Provider在
  2. Android开发环境搭建,各版本系统下androi
  3. Android 5.1截获HOME键
  4. Android P Camera架构
  5. Android(安卓)数据存储——数据查询query
  6. Android(安卓)编程下设置 Activity 切换
  7. 获得a meta-data 的值
  8. android:ClassNotFoundException for Act
  9. Android 文件及文件夹操作
  10. Android数据解析JSON解析之手动JSON解析