如何始终在后台运行服务? [英] How to always run a service in the background?

查看:192
本文介绍了如何始终在后台运行服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建与内置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.

推荐答案

始终在其中运行的服务 背景

a service that is always running in the background

这是不可能从您所发现的术语的任何实际意义上讲.这也是不良设计.

This is not possible in any real sense of the term, as you have discovered. It is also bad design.

每5分钟一次.服务检查 设备的当前位置和 调用网络服务

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

使用AlarmManager.

使用AlarmManager使5 分钟间隔调用服务.但是我 无法完成这项工作.

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

这是一个示例项目,显示了如何使用一个,以及 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.

这篇关于如何始终在后台运行服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆