如何让我的整个应用程序在后台运行?
我制作了一个应用程序,它只是一个简单的计数器,可以跟踪用户打高尔夫球时的击球情况并在打高尔夫球时保存分数。我只是希望当用户退出时应用程序不会被杀死。我只是想让它在后台运行。因此,用户可以在做其他事情(例如发短信或检查电子邮件)后重新进入程序
我知道我需要使用服务,但在尝试在网络和其他论坛上研究该主题时,似乎他们都解释了如何让应用程序中的特定活动继续,而不是整个应用程序。
我该怎么做?
I made an app that is just a simple counter that will keep track of a user's strokes as they play golf and save their score while doing it. I simply want the app to not be killed when the user exits out. I just want it to run in the background. So the user can re-enter the program after doing other things, (like texting or checking email)
I understand I need to use services but while trying to research the topic on the web and in other forums, it seems that they all explain how to have a specific activity in the app to continue, not the entire app.
How might I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你可以做这样的事情
并通过做得到分数
You could do something like this
and get the score back by doing
您应该将数据存储到永久存储中,而不是尝试无限期地使应用程序保持活动状态(无论如何都不能 100% 实现):
http://developer.android.com/guide/topics/data/data-storage.html
You should just store data to permanent storage instead of trying to keep the app alive indefinitely (which can not be 100% achieved anyway):
http://developer.android.com/guide/topics/data/data-storage.html
我想指出 Ben Williams 基本上是正确的,但他在保存文件中错过了一个步骤 (editor.commit();)。在提交之前,数据不会存储在设置文件中:
我建议查看 http://developer.android.com/reference/android/app/Activity.html 查看生命周期(确切地知道在什么情况下你想把它放在哪里)。
I would like to point out that Ben Williams was mostly correct, but he missed a step (editor.commit();) in the save file. The data does not get stored in the settings file until it is committed:
I suggest looking at http://developer.android.com/reference/android/app/Activity.html to see the lifecycle (to know exactly where you want to put it for what situation).
您可以将其作为 Windows 服务应用程序。
You can make it as a windows service application.