继续学习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,但它现在看起来不太好=(

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


当前回答

使用以下代码:

Intent i = new Intent();
i.setAction(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_HOME);
ListActivity.this.startActivity(i);
finish();

其他回答

您可以通过按Back按钮或在Activity中调用finish()来退出。如果您想显式地终止它,只需从MenuItem调用finish()。

Romain并不是说不能这样做,只是说这样做毫无意义——用户不需要关心是否退出或保存他们的工作,因为应用程序生命周期的工作方式鼓励您编写智能软件,无论发生什么都能自动保存和恢复其状态。

几乎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开发新手,我开始熟悉生命周期等等。作为一名Android用户,我一直讨厌自己无法删除应用程序。

为什么用户应该信任一个应用程序?我们可能认为把应用放在后台是“安全的”,但用户真的安全吗? 我们可能会爱上“新”做事方式的天才,但并不是所有的应用程序都写得很完美,甚至很好。有些可能是恶意的,并试图保持后台进程一直运行。有些可能是出于好意,但很混乱。

我讨厌打开浏览器或谷歌,从我上次离开的地方开始,不得不向后堆叠几十个缓慢的页面,只是为了感觉我有一个干净的开始。用户应该拥有最终的控制权。有多少次技术支持告诉我们“重启我们的机器”或“关闭程序并重新启动”?用户需要感觉他们是在重新启动应用,而不是恢复一个可能会让他们沮丧或给他们带来问题的状态。

你不能指望人们保留一个复杂的环境模型,只是为了使用一个应用程序来完成一些事情。人们觉得自己可以控制铅笔和纸,因为这体现在他们对铅笔和纸的行为和未来行为的体验中。 软件是魔术,它发生在幕后。它的行为规则就像创建它的开发者一样反复无常。

We should try to design appliances that relate to an underlying, almost physical, model that is robust and reliable and truly intuitive to the user. "Killing" an app is something a user can embrace. It's like throwing out a pile of scratch paper and starting over; closing a book and putting it back on the shelf. Magic has its place for dedicated professionals who can invest themselves in a particular world, such as video editing or animation systems. And these users often contribute to the features themselves and so are comfortable with them. But everyday users deserve at least a few really grounded options they can rely on regardless of sophistication level, in my opinion. I'm for an easy way to exit a process completely even if it is not the target model the system aspires to.

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.

这很简单。只要遵循我要告诉你的这些指示:

比如你有多个活动,从一个活动到另一个活动。你可能会像这样使用intent:

Intent i1 = new Intent(this, AnotherActivity);
startActivity(i1) 

你只需要添加finish();例如,从头到尾在每个活动上启动intent活动后,

Intent i1=new Intent(this, AnotherActivity);
startActivity(i1) 
finish();

所以当你点击退出按钮时使用的是finish()或System.exit(0)必须完全关闭你的应用程序。