Android的服务与LocationListener的回调 [英] Android service with locationListener callbacks

查看:147
本文介绍了Android的服务与LocationListener的回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Android应用程序。根据用户当前的地理位置,我要取的一些背景远程数据并将其存储。我的实现是:

在特定的时间间隔一个报警触发了我的服务。服务使用匿名类来查询当前位置并注册一个LocationListener的回调。在onLocationChanged()呼叫我开始从服务器远程数据读取。

然而,一旦我的服务是通过使用anonymos类注册位置监听器,它返回预期;因为它没有等待回调结束前发生。由于回调需要一些时间,使呼叫时的服务已经恢复,它抛出一个错误说:

  java.lang.RuntimeException的:处理器{43e82510}发送消息到一个处理程序上的死线
 

这是完全可以理解的。一个快速的解决方法,我现在是,我可以用getLastKnownLocation从locationManager为不通过回调进行回应;但是如果我想要什么最新的位置,现在,在服务,而不是活动?我如何可以等待回调发生,从返回停止我的服务。

此外,在什么时候lastKnownlocation得到更新?每次GPS注册了一个新的位置;它更新呢?我想知道的是,如果它不是最新的可它仍然封闭最新消息?由于我没有看到Android模拟器的选项来配置后续更新之间的时间段。

任何帮助是非常AP preciated。 干杯

解决方案
  

但如果我想要最新的位置,现在,在服务,而不是活动?

抱歉,但这是不可能的,在任何一个服务或活动。例如,如果GPS无线电关闭,并正在从GPS请求位置数据,将需要几十秒只是为了获得一个解决方法,并且如果你是幸运的。它可能无法得到修复的。

  我

如何可以等待回调发生,从返回停止我的服务。

您不知道。你做,你说你会做什么:

  

使用getLastKnownLocation从locationManager为不通过回调进行回应

因此​​,有你的服务(这是希望的 IntentService )检查是否 getLastKnownLocation()恰好有一个值。如果是这样,使用它。否则, registerLocationUpdates()使用 PendingIntent 将控制传递回你的 IntentService 。当你的意图,使用位置和注销更新(假设报警时间是好的,长一样,也就是说,一小时一次)。

事情变得棘手,如果你的报警器是一个_WAKEUP报警。然后,您需要持有 WakeLock ,因此该设备将无法退回睡着了,而你正试图获得GPS定位。但是,你需要释放 WakeLock 的某个时候,如果我们无法获得GPS定位......嗯......好,这是最棘手的部分。试图找出处理这是一个很好的清洁方式,并实施它作为一个可重用的组件(例如, LocationAlarmService ),为18000项对我的待办事项列表中的一个。

  

此外,在什么时候lastKnownlocation得到更新?每次GPS注册了一个新的位置;它>更新呢?

AFAIK,是的。

I have an android application. Based on the current geo location of user, I want to fetch some remote data in background and store it. My implementation is:

At specific interval a alarm fires up my service. Service uses an anonymous class to query current location and registers a locationListener callback. On call of onLocationChanged() I initiate the remote data fetch from server.

However once my service is done registering the location listener using anonymos class, it returns as expected; as it doesn't wait for callback to happen before finishing. Since callback takes some time and makes a call when service has already returned, it throws an error saying:

java.lang.RuntimeException: Handler{43e82510} sending message to a Handler on a dead thread

Which is quite understandable. One quick workaround for me now is that I can use getLastKnownLocation from locationManager as that doesn't respond back by callback; but what if I do want the latest location right now, in a service and not activity? How can I wait for callback to happen and stop my service from returning.

Also, at what point does lastKnownlocation gets updated? Everytime GPS registers a new location; does it update it? What I want to know is that if it's not latest can it still be closed to latest? As I didn't see an option in android emulator to configure the time period between subsequent updates.

Any help is much appreciated. Cheers

解决方案

but what if I do want the latest location right now, in a service and not activity?

Sorry, but that is not possible, in either a service or an activity. For example, if the GPS radio is off, and you are requesting location data from GPS, it will take tens of seconds just to get a fix, and that's if you are lucky. It might not get a fix at all.

How can I wait for callback to happen and stop my service from returning.

You don't. You do what you said you would do:

use getLastKnownLocation from locationManager as that doesn't respond back by callback

So, have your Service (which is hopefully an IntentService) check to see if getLastKnownLocation() happens to have a value. If it does, use it. Otherwise, registerLocationUpdates() using a PendingIntent that will pass control back to your IntentService. When you get that Intent, use the location and unregister for updates (assuming the alarm period is nice and long, like, say, once an hour).

Things get tricky if your alarm is a _WAKEUP alarm. You will then need to hold a WakeLock, so the device does not fall back asleep while you are trying to get a GPS fix. However, you need to release that WakeLock sometime, and if we cannot get a GPS fix...ummm...well, that's the tricky part. Trying to figure out a nice clean way of handling this, and implementing it as a reusable component (e.g., LocationAlarmService), is one of 18,000 items on my to-do list.

Also, at what point does lastKnownlocation gets updated? Everytime GPS registers a new location; does it > update it?

AFAIK, yes.

这篇关于Android的服务与LocationListener的回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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