System.exit(0)和System.exit(1)区别

1.参考文献

http://hi.baidu.com/accpzhangbo/blog/item/52aeffc683ee6ec238db4965.html

2.解析

查看java.lang.System的源代码,我们可以找到System.exit(status)这个方法的说明,代码如下:

  /**
     * Terminates the currently running Java Virtual Machine. The
     * argument serves as a status code; by convention, a nonzero status
     * code indicates abnormal termination.
     * <p>
     * This method calls the <code>exit</code> method in class
     * <code>Runtime</code>. This method never returns normally.
     * <p>
     * The call <code>System.exit(n)</code> is effectively equivalent to
     * the call:
     * <blockquote><pre>
     * Runtime.getRuntime().exit(n)
     * </pre></blockquote>
     *
     * @param      status   exit status.
     * @throws  SecurityException
     *        if a security manager exists and its <code>checkExit</code>
     *        method doesn't allow exit with the specified status.
     * @see        java.lang.Runtime#exit(int)
     */
    public static void exit(int status) {
    Runtime.getRuntime().exit(status);
    }
注释中说的很清楚,这个方法是用来结束当前正在运行中的java虚拟机。如何status是非零参数,那么表示是非正常退出。

  1. System.exit(0)是将你的整个虚拟机里的内容都停掉了 ,而dispose()只是关闭这个窗口,但是并没有停止整个application exit() 。无论如何,内存都释放了!也就是说连JVM都关闭了,内存里根本不可能还有什么东西
  2. System.exit(0)是正常退出程序,而System.exit(1)或者说非0表示非正常退出程序
  3. System.exit(status)不管status为何值都会退出程序。和return 相比有以下不同点:   return是回到上一层,而System.exit(status)是回到最上层

3.示例

在一个if-else判断中,如果我们程序是按照我们预想的执行,到最后我们需要停止程序,那么我们使用System.exit(0),而System.exit(1)一般放在catch块中,当捕获到异常,需要停止程序,我们使用System.exit(1)。这个status=1是用来表示这个程序是非正常退出。


### 使用BroadcastReceiver与System.exit(0)实现应用关闭或处理特定广播事件 在 Android 中,`BroadcastReceiver` 是用于响应来自系统或其他应用程序的广播消息的一种机制。通过 `BroadcastReceiver` 可以监听各种类型的广播事件并执行相应的操作。如果需要结合 `System.exit(0)` 来实现程序退出或者处理某些特定广播事件,则可以按照以下方式设计。 #### 广播接收器的基础设置 创建一个继承自 `BroadcastReceiver` 的类,并重写其 `onReceive()` 方法,在该方法中定义接收到广播后的具体行为[^3]。例如: ```java public class ExitAppReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if ("com.example.EXIT_APP".equals(intent.getAction())) { System.exit(0); } } } ``` 上述代码片段展示了当接收到指定动作 `"com.example.EXIT_APP"` 的广播时,会触发 `System.exit(0)` 关闭整个 JVM 实例的行为。 #### 动态注册广播接收器 可以通过 Java/Kotlin 代码动态注册广播接收器来捕获特定意图(Intent)。以下是动态注册的一个例子: ```java // 创建广播接收器实例 ExitAppReceiver exitAppReceiver = new ExitAppReceiver(); // 定义过滤条件 IntentFilter filter = new IntentFilter(); filter.addAction("com.example.EXIT_APP"); // 注册广播接收器 registerReceiver(exitAppReceiver, filter); ``` 此部分实现了将 `ExitAppReceiver` 对象绑定到当前上下文中,并指定了它所关注的动作名称为 `"com.example.EXIT_APP"`[^1]。 #### 静态声明广播接收器 另一种更持久化的方式是在 `AndroidManifest.xml` 文件里静态配置广播接收器及其感兴趣的意图过滤规则。如下所示: ```xml <receiver android:name=".ExitAppReceiver"> <intent-filter> <action android:name="com.example.EXIT_APP"/> </intent-filter> </receiver> ``` 这种方式使得即使应用未运行也能对外部发送的相关广播做出反应[^2]。 需要注意的是,调用 `System.exit(0)` 虽然能够强制终止虚拟机进程从而达到结束应用的效果,但这并不是推荐的做法,因为这可能会绕过正常的组件销毁流程而导致资源泄漏等问题。通常建议采用标准 API 如 `finishAffinity()`, 或者通知 Activity/Service 自己去完成清理工作后再自行退出。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值