在后台服务中使用GoogleAPIClient [英] Using GoogleAPIClient in Background Services

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

问题描述

我的情况是,我希望有一个服务定期获取一些信息(例如,用户的位置),将其报告给我的服务器,如果该位置与某个X位置匹配,则接收推送通知.

My scenario is that I would like to have a service periodically obtain some information (eg. location of user), report it to my server and receive a push notification if the location matches some X location.

无论应用程序是否运行,我都会就什么是定期检索位置信息的最佳机制提供一些建议.这是我考虑过的选项以及每个选项的一些背景信息.

I would to get some advice on what would be the best mechanism to periodically retrieve location information regardless of whether the application is running or not. Here are the options that I have thought about and some background information on each.

创建标准的Android服务

我的第一个想法是创建一个STICKY服务,该服务初始化一个GoogleAPIClient来获取位置更新,并且一旦接收到位置,它将把它发送到服务器.这种方法的问题在于,我必须定期获取位置信息.因此,即使未运行位置扫描,该服务也将一直运行.还有,在服务中如此频繁地轮询位置的最佳方法是什么?

My first thought was to create a STICKY service that initializes a GoogleAPIClient that will get location updates and once a location is received, it will send it to the server. The problem I have with this approach is that I have to periodically get the location information. So the service would be running constantly even if it is not running a location scan. Also what would be the best way to poll for location every so often within a service?

使用IntentService创建警报

我想到的第二个选项是唤醒警报管理器并启动IntentService,以启动位置更新(通过GoogleAPIClient).一旦获得位置,请发送到服务器并关闭位置更新.这将解决定期启动/停止位置更新的问题.但是,在没有等待的情况下,我在IntentService中连接到GoogleAPIClient的运气并不好,我认为这不是最有效的方法.

The second option I thought about was having the Alarm Manager wake up and start an IntentService which starts location updates (via GoogleAPIClient). Once a location is obtained send to the server and close the location updates. This would solve the issue of periodically starting/stoping location updates. However, I haven't had much luck connecting to GoogleAPIClient within the IntentService without putting a wait and I don't think that is the most efficient thing to do.

创建GcmTaskService

这具有AlarmManager的许多优点,并且在休眠模式下似乎更有效,因为它仅在电话处于维护模式时才唤醒服务,因此这似乎是一个不错的解决方案.但是,与IntentService一样,我也遇到了同样的问题,后者正在连接到GoogleAPIClient以获取位置,而无需进行任何等待.

This has many advantages of the AlarmManager and seems to be more efficient in terms of doze mode as it only wakes up the Service whenever the phone is in a maintenance mode so it seemed like a good solution. However, I have the same issue as the IntentService which is connecting to a GoogleAPIClient to get location without some sort of wait.

我可能在这只兔子的洞里走得太远了,可能我遗漏了一些明显的东西,所以任何建议都将不胜感激.

I may be going too far down the rabbit hole on this one and there may be something obvious I am missing so any advice would be great appreciated.

推荐答案

我遇到了同样的问题.我采用第一种方法(粘性服务),并且一切正常.但是,我不喜欢该服务始终在运行并且对用户可见的事实. 这就是为什么我决定使用JobScheduler API,因为根据文档.

I had same problem. I went for the first approach (a sticky service) and everything worked fine. However, I didn't like the fact that the service was always running and visible to the user. That's why I decided to use the JobScheduler API as it's the preferred way of performing background work according to docs.

如果您的应用程序的目标API> = 21,那么JobScheduler是您的理想之选.如果要支持该平台的较早版本,则应使用最近推出的 Firebase JobDispatcher 库,遗憾的是缺少文档.

If your app targets API>=21 then JobScheduler is the way to go. If you want to support older versions of the platform then you should use the recently introduced Firebase JobDispatcher library, which unfortunately lacks of documentation.

在Google I/O 2016演示文稿上此处介绍了所有内容.看看吧.

Everything is explained here at a Google I/O 2016 presentation. Take a look at it.

这篇关于在后台服务中使用GoogleAPIClient的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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