Android的Locationprovider时间过长暂时不可用 [英] Android Locationprovider takes too long to become temporarily unavailable

查看:222
本文介绍了Android的Locationprovider时间过长暂时不可用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据这个文章,设置的 minTime 的请求时,位置更新将导致供应商将自身设置为TEMPORARILY_UNAVAILABLE为的 minTime 的毫秒,以节省电池电量。在此期间不活动时,GPS供应商将自行关闭和GPS图标会消失。

在我的code,我设置了minTime为30秒左右,但供应商才能成为TEMPORARILY_UNAVAILABLE每五分钟一次。当它,它最多只重新打开自己之前保持TEMPORARILY_UNAVAILABLE十秒钟。我知道这是因为GPS图标再次重现之前只有十秒钟后消失。

我明白的 minTime 的设置只针对Android位置提供一个粗略的指南......但我pretty确保五分钟30秒完全不同。有谁知道是怎么回事呢?怎样的 minTime 的和requestLocationUpdates实际工作?

的LocationManager设置:


  locManager =(的LocationManager)getSystemService(LOCATION_SERVICE);
locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,30000L,0,locListener);


locListener:

 公共无效onLocationChanged(位置LOC){
    //跟踪最佳位置
    //有一个地点>没有位置
    如果(bestLocation == NULL)
        bestLocation =禄;
    //更多的准确性>精度较差
    否则如果(loc.getAccuracy()&下; = bestLocation.getAccuracy())
        bestLocation =禄;    Log.d(TAG,更新的位置;
}
公共无效onStatusChanged(字符串提供商,INT地位,捆绑演员){
    Log.d(TAG,新状态:+状态);
    如果(状态== LocationProvider.TEMPORARILY_UNAVAILABLE)
        //做的东西,因为供应商暂时关闭
}

一个真实的Andr​​oid设备的调试输出(HTC不可思议2.2):

 位置更新
更新位置
新状态:2
更新位置
更新位置
更新位置
新状态:2
......(五分钟后)
新状态:1


解决方案

从<一个href=\"http://developer.android.com/reference/android/location/LocationManager.html#requestLocationUpdates%28long,%20float,%20android.location.Criteria,%20android.app.PendingIntent%29\"相对=nofollow> API :

后台服务应该小心设置足够高的minTime使设备不通过保持GPS或所有的时间无线设备消耗了太多的力量。尤其是,下的60000毫秒值不推荐(重点是我的)

这个字段只用作提示,以节省电力,和位置更新之间的实际时间可能比这个数值大或小的。 (重点是我的)。

如果您需要立即知道位置,使用<一个href=\"http://developer.android.com/reference/android/location/LocationManager.html#getLastKnownLocation%28java.lang.String%29\"相对=nofollow> getLastKnownLocation(字符串)

According to this article, setting the minTime when requesting location updates will cause the provider to set itself to TEMPORARILY_UNAVAILABLE for minTime milliseconds in order to conserve battery power. During this period of inactivity, the GPS provider will turn itself off and the GPS icon will disappear.

In my code, I set the minTime to about 30 seconds, but the provider only becomes TEMPORARILY_UNAVAILABLE once every five minutes. When it does, it only stays TEMPORARILY_UNAVAILABLE for ten seconds at most before turning itself back on. I know this because the GPS icon disappears for only ten seconds before reappearing again.

I understand that the minTime setting is only a rough guideline for the Android location provider...but I'm pretty sure that five minutes is entirely different from 30 seconds. Does anyone know what is going on here? How does minTime and requestLocationUpdates actually work?

LocationManager Setup:

locManager = (LocationManager) getSystemService(LOCATION_SERVICE);
locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,30000L, 0, locListener);

locListener:

public void onLocationChanged(Location loc) {
    //Keep track of best location
    //Having a location > no location
    if (bestLocation == null)
        bestLocation = loc;
    //More accuracy > Less accuracy
    else if (loc.getAccuracy() <= bestLocation.getAccuracy())
        bestLocation = loc;

    Log.d(TAG, "Location Updated";
}
public void onStatusChanged(String provider, int status, Bundle extras) {
    Log.d(TAG, "New status: " + status);
    if (status== LocationProvider.TEMPORARILY_UNAVAILABLE)
        //Do stuff since the provider is temporarily off
}

Debug output on a real Android device (HTC Incredible 2.2):

Location Updated
Location Updated
New status: 2
Location Updated
Location Updated
Location Updated
New status: 2
... (five minutes later)
New status: 1

解决方案

From the API:

"Background services should be careful about setting a sufficiently high minTime so that the device doesn't consume too much power by keeping the GPS or wireless radios on all the time. In particular, values under 60000ms are not recommended." (emphasis is mine)

"This field is only used as a hint to conserve power, and actual time between location updates may be greater or lesser than this value." (emphasis is mine).

If you need to know the location immediately, use getLastKnownLocation(String)

这篇关于Android的Locationprovider时间过长暂时不可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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