继续学习Android,我读了以下内容:

Question: Does the user have a choice to kill the application unless we put a menu option in to kill it? If no such option exists, how does the user terminate the application? Answer: (Romain Guy): The user doesn't, the system handles this automatically. That's what the activity lifecycle (especially onPause/onStop/onDestroy) is for. No matter what you do, do not put a "quit" or "exit" application button. It is useless with Android's application model. This is also contrary to how core applications work.

呵呵,我在Android世界里每走一步都会遇到一些问题=(

显然,你不能在Android中退出应用程序(但Android系统可以随时完全销毁你的应用程序)。这是怎么回事?我开始认为这是不可能写一个应用程序的功能作为“正常的应用程序”-用户可以退出应用程序时,他/她决定这么做。这不应该依靠操作系统来完成。

我正在尝试创建的应用程序不是Android市场的应用程序。它不是一个被大众“广泛使用”的应用程序,它是一个将在非常狭窄的商业领域使用的商业应用程序。

我其实很期待为Android平台开发,因为它解决了Windows Mobile和。net中存在的许多问题。然而,上周对我来说有点令人失望……我希望我不必放弃Android,但它现在看起来不太好=(

有办法让我真的退出应用程序吗?


当前回答

All of my applications have quit buttons... and I quite frequently get positive comments from users because of it. I don't care if the platform was designed in a fashion that applications shouldn't need them. Saying "don't put them there" is kind of ridiculous. If the user wants to quit... I provide them the access to do exactly that. I don't think it reduces how Android operates at all and seems like a good practice. I understand the life cycle... and my observation has been that Android doesn't do a good job at handling it.... and that is a basic fact.

其他回答

如果没有退出函数,应用程序开发人员就可以杀死自己的应用程序,这是非常糟糕的设计。

我的应用程序需要允许用户在运行时动态地更改数据,用户需要重新启动我的应用程序才能使更改生效,但Android不允许我的应用程序自行重新启动。Android操作系统有一个非常糟糕的设计应用程序生命周期。

几乎99%的情况下,Android应用程序不需要接管自己的生命周期。大多数情况下,这归结于更好的计划或更聪明的应用程序设计。例如,构建一个内部服务(不导出)来处理下载等,或者围绕用户工作流设计动作和任务。

但话虽如此,有志者事竟成。Android通过Android .os. process类提供了一个比Java更好的API来控制底层进程。与Java不同的是,它不会把开发人员当成傻瓜,把所有问题都隐藏在一个简单的Java .lang. system .exit()调用之后。

那么如何让你的应用在Android中自杀呢?诀窍很简单:

通过继承标准Android .app. application类来创建自己的Android应用程序类(记得在AndroidManifest.xml文件中声明它)。

重写onCreate()方法,并存储启动应用程序的进程ID:

this.pid = android.os.Process.myPid(); // Save for later use.

现在要杀死你的应用程序,提供一个kill()方法:

android.os.Process.sendSignal(pid, android.os.Process.SIGNAL_KILL);

现在,无论何时你需要你的应用自杀,只要输入转换应用上下文,并调用你的kill方法!

((MySuicidalApp) context.getApplicationContext()).kill()

请记住,由于Android中的进程管理策略,特别是与服务相关的策略,Android可能只是选择重新启动你的服务(参见你不应该在Android上使用任务杀手)。

当我在Android上构思一个应用程序时,我是这样看的:

您正在使用您的应用程序 电话响了 你接电话 在通话结束时,您将回到应用程序所在的位置

要做到这一点,你只需要手机的后退键或Home键(短按或长按)和通知栏。

当我退出我的应用程序时,我只使用后退按钮,直到我退出它或Home按钮。

我认为这就是大多数应用的构思。但如果我需要某种会话或连接,我会用登录/注销按钮和通知(标题栏或其他任何东西)向用户明确表示。这是一种与纯“退出”样式应用程序相当不同的样式。

在pc上,你有一个多gui桌面,在Android上,你显然有多个任务,但你一次只显示一个应用程序(这里我不考虑小部件^^)。在手机上,任何时候,你都可能收到比你正在做的事情更重要的通知。

因此,应用程序的整个概念依赖于不同的东西,即“进入应用程序-工作-退出应用程序”。

有退出按钮的一个重要原因是“退出”广告。在出口,可以显示一些产生收入的广告。像所有的广告一样,它还是有点烦人,但也许比那些占用宝贵屏幕空间的广告更烦人。一些广告网络提供这种广告方式。但是,真的,你不能只是放一个退出按钮,在显示广告后什么都不做!

因此,在某些情况下需要一种或另一种方式来终止程序,而“永远不应该被需要”可能不是最全面的答案。

可能会使用Activity.finish()或System.exit(0)。

博客文章“何时在Android应用程序中包含退出按钮”(提示:永远不要)解释得比我好得多。我希望每个Android开发者都读过这本书。

摘录:

In my experience what [the users] really want is: An unambiguous way to guarantee that an app will stop consuming resources (battery, CPU cycles, data transfer, etc.). Many users perceive that an exit button implements this requirement and ask for it to be added. Developers, looking to please their users, obligingly add one. Shortly thereafter they both fail. In most cases the exit button simply calls Activity.finish(). This is exactly equivalent to hitting the back button. Exactly. Services keep running and polling keeps happening. Users may think they've killed the app but they haven't, and soon they'll be even more annoyed. Exit behavior is now ambiguous. Should your exit button just close the Activity, or should it also stop all associated Services, Receivers, and Alarms? What should Back do? What happens if they hit Home instead? What happens if your app has a widget? Should the exit button stop that from updating too? The solution is to make the back button behave as you'd expect the exit button to. Better yet, simply stop consuming resources whenever the app isn't visible.

继续阅读完整的文章。