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

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


当前回答

几乎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开发者都读过这本书。

摘录:

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.

继续阅读完整的文章。

显然,您已经在finish()命令中找到了想要的答案。这不会将你的应用从内存中移除,但Android会在它需要资源的时候这样做,所以你不会显式地这样做没有任何区别。

我只想补充一点,为了达到应用程序退出通常具有的全部效果,您需要将应用程序的状态重置为设备引导后第一次运行时的正常状态,就在对所有活动调用finish()之前。这样,如果用户再次选择你的应用程序,它将显示为“新鲜”运行,在模拟“退出”之前没有任何状态。

如果有一些特殊的操作应该只在“退出”时发生,比如保存用户的工作或其他什么,您也可以在上述例程的重新初始化部分之前执行它们。

这种方法可以让你实现“退出”命令的目标,而不违反Android的理念,即将操作系统资源的管理(包括关闭应用程序)交给操作系统。

就我个人而言,我不会使用这种方法,因为Android用户希望应用程序在他们重新访问时保持其连续性,因此他们不习惯“退出”应用程序的模式。相反,我支持“清除”函数,用户可以调用该函数将应用程序重置为某个默认的初始状态,而无需在此过程中“离开”它。

唯一的例外是,当用户按下后退按钮的次数足够多,导致应用程序关闭。在这种情况下,用户不希望状态被保存(如果应用程序中有未保存的状态,那么作为开发人员,你应该有代码处理返回按钮,检测未保存的数据,并提示用户将其保存到SharedPreferences或文件,或其他一些非易失性介质)。

关于system . exit (0):

如果你决定使用system.exit(0)关闭你的应用程序与粗鲁的终结(例如,作为一个最后的后退按钮按下的结果),那么我要警告你,虽然对我来说这是“工作”,在某些情况下,我已经能够关闭一个应用程序没有任何痕迹,有一个小故障发生在果冻豆当你使用这种方法。

具体来说,如果你使用最近的应用程序列表打开你的应用程序,然后使用返回按钮关闭应用程序(通过system.exit(0)实现关闭),最近的应用程序列表将再次可见,因为它永远不会被关闭。如果你在该列表中点击应用程序的条目,从相同的、已经打开的、最近的应用程序列表中再次运行它,将没有响应。

我怀疑造成这种情况的原因是,最近的应用程序列表持有一个对你的应用程序的引用,由于你已经使用system.exit(0)关闭了应用程序,该应用程序已变得无功能。使用finish()更文明地关闭应用程序可能会通知操作系统,允许它刷新其最近应用程序列表,但system.exit(0)显然没有这样做。

This is not a huge problem in and of itself, as very few people will open an app from Recent Apps, then exit it, and then immediately open it again from the same open Recent Apps list. And if they tap the home button and then re-open the Recent Apps list, your app's entry will be there, and it will be fully functional. But I think that it shows that the use of system.exit(0) can interfere with proper communication between your app and the OS, and this suggests that there may be other, more serious, possibly subtle, consequences of using this approach.

嗯……

我认为你只是没有以正确的方式看待Android应用。你可以很容易地做一些你想做的事情:

像开发者生命周期文档中鼓励的那样,执行应用程序活动的保存/恢复状态。 如果在恢复阶段需要一些登录(没有可用的登录/会话信息),那么就这样做。 最终添加一个按钮/菜单/超时,在这种情况下,你将执行finish()而不保存登录和其他会话信息,隐式地结束应用程序会话:因此,如果应用程序再次启动/带到前面,它将开始一个新的会话。

这样你就不关心应用是否真的从内存中删除了。

如果你真的想从内存中删除它(这是不鼓励的,顺便问一下,为了什么目的?),你可以在onDestroy()的末尾有条件地杀死它,使用java.lang.System.exit(0)(或者restartPackage(..)?)当然,只有在你想“真正结束应用程序”的情况下才这样做,因为onDestroy()是活动正常生命周期的一部分,而不是应用程序的结束。

Android上下文中的应用程序只是一堆模糊相关的活动,退出应用程序并没有多大意义。你可以finish()一个Activity, Activity栈中前一个Activity的视图就会被绘制出来。

对于应用程序的第一个(启动)活动,

@Override
public void onBackPressed(){

    // Exit
    moveTaskToBack(true);
}

为我工作。我想在这里关闭应用程序。从其他活动中回来;我用了意图,例如:

@Override
public void onBackPressed(){

    // Going back....
    Intent intent = new Intent(ActivityB.this, ActivityA.class);
    startActivity(intent);
    finish();
}

注意:这段代码适用于开发人员想要从ActivityZ返回ActivityA然后关闭应用程序的场景。