android - 如何始终在后台运行服务?

标签 android android-service background-thread

我正在创建一个类似于内置短信应用的应用。

我需要什么:

  • 始终在后台运行的服务
  • 每 5 分钟一次。该服务检查设备的当前位置并调用网络服务
  • 如果满足某些条件,服务应生成通知(就像 SMS 应用程序一样)
  • 当点击通知时,用户被带到应用程序(就像短信应用程序一样)
  • 安装应用后,应启动服务
  • 设备重启后,服务应该会启动

我尝试过的:
- 运行正常的服务,直到 Android 终止服务
- 使用 AlarmManager 制作 5 分钟。对服务的间隔调用。但我无法完成这项工作。

最佳答案

a service that is always running in the background

这是not possible正如您所发现的那样,在任何真正意义上的术语中。也是bad design .

every 5 min. the service checks the current location of the device and calls a web service

使用AlarmManager

using the AlarmManager the make the 5 min. interval call to a service. But I was not able to make this work.

这里是 sample project显示如何使用一个,以及使用 WakefulIntentService所以你在尝试做整个 Web 服务的时候保持清醒。

如果您仍然遇到问题,请针对您在使用 AlarmManager 时遇到的具体问题提出新问题。

关于android - 如何始终在后台运行服务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2566350/

相关文章:

java - wso2 mb 2.2.0 启动错误

java - 如何在用户强制停止后自动重新启动服务

服务中未显示 android 通知

iphone - IOS后台工作

后台线程中的android addView

android - 存储在 SQLite 数据库中

java - 添加按钮、图像等时,它们不会显示在 Android Studio 中

Android 底部工具栏像折断

android - 执行doInBackground()时出现java.lang.RuntimeException错误如何解决?

android - 单击 android 中的“快速设置”磁贴后关闭“快速设置”面板