是System.exit(0)真的有那么危险吗? [英] Is System.exit(0) really that dangerous?

查看:3625
本文介绍了是System.exit(0)真的有那么危险吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是应用程序的后台服务,更新SQLite数据库。因此,我的活动正在变得过时。活动的意图也包含过时PARAMS这样的onCreate,onResume会崩溃的应用程序。一个最简单的办法就是重新启动整个应用程序。我不想加入国际单项体育联合会对所有的onCreate,onResume方法的所有活动,以处理一个特殊情况。

An application background service updates sqlite database. Therefore my activities are becoming outdated. Activity intents also contain outdated params so onCreate, onResume will crash the application. An easiest solution is to restart whole application. I don't want to add IFs to all onCreate, onResume methods in all activities to handle one special case.

我注意到 ACRA 有以下异常被处理后,code执行。

I noticed that ACRA has following code executed after an exception has been handled.

android.os.Process.killProcess(android.os.Process.myPid());
System.exit(10);

然而,许多人不鼓励使用的System.exit(0)。为 System.exit(0)为Android应用程序的数据完整性真的有那么危险吗?当然,我的code将现有之前关闭数据库。

However many people discourage use of System.exit(0). Is System.exit(0) really that dangerous for an Android application data integrity? Of course my code will close the database before existing.

更新:

我知道如何使用完成(),内容提供商,发送广播,这里SO看了很多答案等,但这些方法都需要额外的数千行code。我实现的解决方案, System.exit(0)十分钟。重启是如此之快,它与普通startActivity动作没有什么区别。该数据库更新/重启更长的用户不活动后这样做的应用程序已经中止系统。我的应用程序不要求实时同步。在测试过程中的应用程序正确的行为。这是快速和肮脏的解决方案。

I known how to use finish(), content providers, send broadcasts, read many answers here on SO, etc. However each of these approaches requires additional thousands lines of code. I implemented solution with System.exit(0) in ten minutes. The restart is so fast that it is indistinguishable from ordinary startActivity action. The db update/restart is done after longer user inactivity so the app is already suspended by the system. My app doesn't require real time syncing. During tests the application behaves correctly. This is quick and dirty solution.

所以我问了一下可能带来的副作用的问题System.exit(0)。不是我怎么可以做不同的设计。我知道,现在的设计是不完美的。

Therefore I asked the question about possible side effects of System.exit(0). Not how I can do the design differently. I know that current design is not perfect.

推荐答案

System.exit(0) Java中的神器运行时,它并不意味着安卓。因此在任何情况下,使用这将是最严重的溶液

System.exit(0) is an artifact from Java runtime, it isn't meant for Android. So in any cases using it would be worst solution.

你为什么不使用 Activity.finish( )优雅?

如果你终止你是生活在这个过程中,你会失去大部分的缓存,并重新启动时间(〜简历用户的眼睛),它下一次会更高。

If you terminate the process you are living in, you'll loose most of the caching and restart time (~resume in the eyes of the user) for it next time will be higher.

了解更多关于Android开发 活动生命周期的文档。

Read more in Activity Lifecycle documentation on Android Developers.

这篇关于是System.exit(0)真的有那么危险吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆