继续学习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开发新手,我开始熟悉生命周期等等。作为一名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.
我同意泰德的观点。我明白退出应用程序不是
“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.
另一个选项可以是Android辅助服务,绿色应用程序正在使用强制关闭应用程序来加速内存。有了你的应用程序辅助服务访问,你可以点击按钮,基本上Greenify应用程序点击强制关闭按钮,在应用程序的设置中发现:
在这里你可以学习无障碍服务:
https://developer.android.com/reference/android/accessibilityservice/AccessibilityService.html
下面是可访问性服务以编程方式点击的设置按钮:
所以你可以通过以下步骤实现杀死任何应用程序,包括你的:
1)登记无障碍服务申请
2)根据您的要求,如果您想杀死所有应用程序获取列表的所有包
3)导航到他们的设置屏幕并单击强制关闭按钮
这是它。我可以分享一个示例代码,我还创建了一个应用程序,如greenify作为家庭作业。
谢谢你!
更新:
“用户不需要,系统会自动处理。”
所以基本上,通过这个解决方案,我们间接地使用了系统强制关闭,但基于用户需求。这样双方都能保持快乐:-)
当我在Android上构思一个应用程序时,我是这样看的:
您正在使用您的应用程序
电话响了
你接电话
在通话结束时,您将回到应用程序所在的位置
要做到这一点,你只需要手机的后退键或Home键(短按或长按)和通知栏。
当我退出我的应用程序时,我只使用后退按钮,直到我退出它或Home按钮。
我认为这就是大多数应用的构思。但如果我需要某种会话或连接,我会用登录/注销按钮和通知(标题栏或其他任何东西)向用户明确表示。这是一种与纯“退出”样式应用程序相当不同的样式。
在pc上,你有一个多gui桌面,在Android上,你显然有多个任务,但你一次只显示一个应用程序(这里我不考虑小部件^^)。在手机上,任何时候,你都可能收到比你正在做的事情更重要的通知。
因此,应用程序的整个概念依赖于不同的东西,即“进入应用程序-工作-退出应用程序”。