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

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

问题描述

我在创建一个应用程序,它类似于集结在SMS应用程序的过程中。

我需要:
- 即总是在后台
运行的服务 - 每5分钟。服务检查设备的当前位置,并调用web服务
- 如果某些条件得到满足,该服务应生成一个通知(就像SMS应用程序)
- 通知被点击时,用户被带到应用(就像SMS应用程序)
- 在安装应用程序时,该服务应启动
- 当设备重新启动,则服务应启动

我曾尝试:
- 运行的工作就好了定期服务,直到机器人杀死服务
- 采用AlarmManager的使5分钟。间隔调用一个服务。但我无法使这项工作。

任何帮助将是很大的AP preciated ...

- 托马斯

解决方案
  

这始终处于运行服务   背景

这是没有任何真正意义上的可能这个词,因为你已经发现。这也是糟糕的设计

  

每隔5分钟。服务检查   该装置的当前位置和   调用Web服务

使用 AlarmManager

  

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

下面是展示了如何使用一个,伴随着一个示例项目使用 WakefulIntentService ,所以你保持清醒,同时试图做全Web服务的事情。

如果你继续这样的问题,开辟了新的问题,关于是给你的悲伤,你遇到与 AlarmManager 具体的事情。

I am in the process of creating an app that is similar to the build-in SMS app.

What I need:
- a service that is always running in the background
- every 5 min. the service checks the current location of the device and calls a web service
- if certain criteria are meet, the service should generate a notification (just like the SMS app)
- when the notification is clicked, the user is taken to the app (just like the SMS app)
- when the app is installed the service should be started
- when the device is rebooted, the service should be started

What I have tried:
- running a regular service which worked just fine until android kills the service
- using the AlarmManager the make the 5 min. interval call to a service. But I was not able to make this work.

Any help would be greatly appreciated...

-Thomas

解决方案

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.

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

Use AlarmManager.

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

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.

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天全站免登陆