如果确定GPS未能获得锁定 [英] Determining if GPS is failing to get a lock

查看:154
本文介绍了如果确定GPS未能获得锁定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

情况:我的程序有大量是基于位置的部分,但这样做,以加快在实际需要的位置,我没有时间来锁定问题。我已经设计的解决方案是让主操作期间的位置信息(读:主屏幕),以使位置时可用特定部分需要它们。

Situation: My program has parts that are heavily location based, but do to speed issues for when the location is actually needed, I don't have time to lock. The solution I have designed is to get location information during primary operation (read: main screen) so that the location is available when the specific pieces need them.

现在我注册更新和onLocationChanged它检查如果位置不够准确。如果它调用removesUpdates。

Right now I register for updates and in onLocationChanged it checks if the location is accurate enough. If it is it calls removesUpdates.

问题:如果设备的地方,它不能获得准确的位置(或任何位置)GPS不关断,打者过热,并得到了用户很不高兴。

Problem: If the device is somewhere that it can not get an accurate location (or any location) the GPS doesn't shut off, the batters overheat, and the users get very upset.

问:是否有一种很好的清洁方法来检查,如果GPS未能获得一个锁,并告诉它在某一点放弃?我创建了一个线程计时器,但由于该请求是绑在onPause / onResume我不想创建额外的线程,可能也没关系。

Question: Is there a good clean way to check if the GPS is failing to get a lock, and tell it to give up at a certain point? I created a thread timer, but because the request is tied to onPause/onResume I do not want to create extra threads that may get all tangled up.

推荐答案

继梅林的回答(我只是打字这个很多了,在记事本的时候才出现) - 我同意。我的建议是:

Further to Merlin's answer (I was just typing this lot out in notepad when it appeared) - I agree. My suggestion is:

面对类似的问题,我决定做的最好的事情是由服务的手段去耦的主要活动整个GPS。

Facing a similar problem, I decided that the best thing to do would be to decouple the whole GPS from the main activity by means of a service.

我的意思是有它执行 requestsLocationUpdates() removeLocationUpdates()并实现了一种服务 LocationListener的。该服务提供了主要的活动可以通过的IBinder 界面的方式调用的方法。它还发送广播来它实现了一个广播接收器来听这些消息。

By this I mean have a service which does the requestsLocationUpdates() and removeLocationUpdates() and implements a LocationListener. This service provides methods which the main activity can call by means of an IBinder interface. It also sends broadcasts to the activity which implements a BroadcastReceiver to listen to these messages.

所以的服务方法的主要活动可致电人会(说)

So one of the the service methods your main activity could call would be (say)

mLocnServ.startGPS(int timeout, float requiredAccuracy, int minUpdatePeriod, int minResendDistance)

其中, mLocnServ 是粘合剂界面由服务暴露出来。

where mLocnServ is the binder interface exposed by the service.

最后两个参数是那些作为参数传递到服务requestLocationUpdates。 (我个人不认为这有什么区别以任何当GPS将自动关闭,据我可以看到它运行所有的时间,直到 removeUpdates()被称为)。但无论如何,第一个参数(超时)应该是你ppared等待需要的精度(参数2)修复$ P $的时间。

The last two arguments being those to pass as arguments to requestLocationUpdates in the service. (personally I don't think these make any difference whatsoever to when the GPS turns itself off, as far as I can see it runs all the time until removeUpdates() is called). Anyway the first argument (timeout) should be the time you are prepared to wait for a fix of the required accuracy (argument 2).

因此​​,在您服务,您需要一个Runnable作为一个定时器,如果超时,西港岛线发送类型的广播 TIMED_OUT (说)和removeUpdates停止GPS 。在你onLocationChanged可以测试位置,你对所要求的精度,如果不够好,送型的广播 GOT_A_FIX (说),并通过位置(纬度/经度和精度)作为在广播演员,然后removeUpdates停止GPS。 ( TIMED_OUT GOT_A_FIX 是枚举它可以弥补广播消息类型来区分只是示例名称)

So in your service you will need a Runnable as a timer, which if it times out wil send a broadcast of type TIMED_OUT (say) and removeUpdates to stop the GPS. In your onLocationChanged you can test the location you get against the required accuracy and if good enough, send a broadcast of type GOT_A_FIX (say) and pass the location (lat/lon and accuracy) as extras in the broadcast, then removeUpdates to stop the GPS. (TIMED_OUT and GOT_A_FIX are just example names for enums which you can make up to distinguish between the broadcast message types)

主要活动可以决定做,然后在其广播接收器的onReceive(什么),即是否要再试一次,如果它有一个 TIMED_OUT 广播或做什么用数据它从 GOT_A_FIX 消息了。

The main activity can decide what to do then in its BroadcastReceiver onReceive(), i.e whether to try again if it got a TIMED_OUT broadcast or what to do with the data it got from the GOT_A_FIX message.

您可能会还需要一个 mLocnServ.stopGPS 中的粘结剂,这样就可以关上了GPS无论什么服务是做

You'll probably also need a mLocnServ.stopGPS in the binder so that you can shut the GPS no matter what the service is doing

这篇关于如果确定GPS未能获得锁定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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