问题描述
我正在创建与内置SMS应用程序相似的应用程序.
I am in the process of creating an app that is similar to the built-in SMS app.
我需要什么:
- 始终在后台运行的服务
- 每5分钟一班.该服务会检查设备的当前位置并调用网络服务
- 如果满足某些条件,则该服务应生成一条通知(就像SMS应用程序一样)
- 点击通知后,用户将被带到应用程序(就像SMS应用程序一样)
- 安装应用程序后,应启动服务
- 重新启动设备后,应启动服务
我尝试过的事情:
-运行常规服务,直到Android终止该服务,该服务才能正常运行
-使用AlarmManager进行5分钟.间隔调用服务.但是我无法完成这项工作.
What I have tried:
- running a regular service which worked just fine until Android kills the service
- using the AlarmManager to make the 5 min. interval call to a service. But I was not able to make this work.
推荐答案
这是不可能从您所发现的术语的任何实际意义上讲.这也是不良设计.
This is not possible in any real sense of the term, as you have discovered. It is also bad design.
使用AlarmManager
.
这是一个示例项目,显示了如何使用一个,以及 WakefulIntentService
的使用,因此您在尝试做整个Web时保持清醒服务的东西.
Here is a sample project showing how to use one, along with the use of WakefulIntentService
so you stay awake while trying to do the whole Web service thing.
如果您仍然遇到问题,请提出一个新问题,询问您与AlarmManager
所遇到的特定事情,这些事情会给您带来悲伤.
If you have continued problems with it, open up a new question about the specific things you are encountering with AlarmManager
that are giving you grief.
这篇关于如何始终在后台运行服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!