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

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


当前回答

我同意泰德的观点。我明白退出应用程序不是 “Android方式”,但它似乎不应该被排除。在这里 您可能需要一个真正的应用程序出口(不是 只是活动):

The user might want some control over which app gets killed in the case of low memory. If important app A is running in the background, then you might like to exit app B when you are done with it so that app A doesn't get killed by the operating system. If your application has sensitive data cached in memory, you might like to kill the app so that a virus/worm/rogue app can't get at it. I know the security model is supposed to prevent that, but just in case... If your application uses resources (like network, CPU, sensors, etc.) that could adversely affect the phone, then one way of ensuring that those resources are freed up is to exit the application. I understand that well-behaved apps should free up resources when they are not needed. But again, exiting the application seems like a reasonable way of ensuring that.

其他回答

Linux内核有一个称为内存不足杀手的特性(如上所述,这些策略可以在用户空间级别进行配置,而且内核不是最优的,但也不是不必要的)。

它被Android大量使用:

OOM杀手不适合用户空间 Android笔记(OOM杀手信息-你可以在Android上配置OOM功能) Android移植在Real Target

一些用户空间应用程序可以帮助这些杀死应用程序,例如:

自动杀手/配置Android内部任务杀手

另一个选项可以是Android辅助服务,绿色应用程序正在使用强制关闭应用程序来加速内存。有了你的应用程序辅助服务访问,你可以点击按钮,基本上Greenify应用程序点击强制关闭按钮,在应用程序的设置中发现:

在这里你可以学习无障碍服务: https://developer.android.com/reference/android/accessibilityservice/AccessibilityService.html

下面是可访问性服务以编程方式点击的设置按钮:

所以你可以通过以下步骤实现杀死任何应用程序,包括你的:

1)登记无障碍服务申请 2)根据您的要求,如果您想杀死所有应用程序获取列表的所有包 3)导航到他们的设置屏幕并单击强制关闭按钮 这是它。我可以分享一个示例代码,我还创建了一个应用程序,如greenify作为家庭作业。 谢谢你!

更新: “用户不需要,系统会自动处理。” 所以基本上,通过这个解决方案,我们间接地使用了系统强制关闭,但基于用户需求。这样双方都能保持快乐:-)

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

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

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

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

不要把您的应用程序看作一个整体应用程序。它是一组UI屏幕,用户可以通过Android服务与你的“应用程序”和“功能”进行交互。

不知道你的神秘应用程序“做什么”并不重要。让我们假设它通过隧道进入某个超级安全的企业内部网,执行一些监控或交互,并保持登录状态,直到用户“退出应用程序”。因为您的IT部门命令它,所以用户必须非常清楚他们何时进入或退出内部网。因此你认为用户“退出”很重要。

这很简单。创建一个服务,在通知栏中放置一个持续通知,说“我在内部网中,或者我正在运行”。让该服务执行应用程序所需的所有功能。拥有绑定到该服务的活动,以允许用户访问与“应用程序”交互所需的UI部分。并有一个Android菜单->退出(或登出,或其他)按钮,告诉服务退出,然后关闭活动本身。

This is, for all intents and purposes exactly what you say you want. Done the Android way. Look at Google Talk or Google Maps Navigation for examples of this "exit" is possible mentality. The only difference is that pressing back button out of your activity might leave your UNIX process lying in wait just in case the user wants to revive your application. This is really no different than a modern operating system that caches recently accessed files in memory. After you quit your windows program, most likely resources that it needed are still in memory, waiting to be replaced by other resources as they are loaded now that they are no longer needed. Android is the same thing.

我真不明白你有什么问题。