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

问题描述

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

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

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

的LocationManager设置:

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时间过长暂时不可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-10 18:15