0

我开发了一个应用程序,每 30 秒后使用当前位置。该应用程序已完成。它可以在 ICS (Android 4)、JellyBean(Android 4.1) 设备上完美运行,但是当我在 Android 2.3 或 Android 2.2 上运行该应用程序时,应用程序 GPS 每隔 2/3 秒就会更新一次并不断更新。虽然我的代码完成了 30 秒,并且在 Android 4,4.1 中完美运行(30 秒后),但在 Android 2.3 或 2.2 中不起作用。

我正在分享代码......

 public class GPSManager
{
    //private static final int gpsMinTime = 60000;
    //private static final int gpsMinDistance = 0;

    private long gpsMinTime = 0;
    private float gpsMinDistance = 0;

    private static LocationManager locationManager = null;
    private static LocationListener locationListener = null;
    private static GPSCallback gpsCallback = null;

    public GPSManager(long gpsMinTime,float gpsMinDistance)
    {   
        this.gpsMinTime = gpsMinTime;
        this.gpsMinDistance = gpsMinDistance;

        GPSManager.locationListener = new LocationListener()
        {
            @Override
            public void onLocationChanged(final Location location)
            {
                if (GPSManager.gpsCallback != null)
                {
                    GPSManager.gpsCallback.onGPSUpdate(location);
                }
            }

            @Override
            public void onProviderDisabled(final String provider)
            {

            }

            @Override
            public void onProviderEnabled(final String provider)
            {

            }

            @Override
            public void onStatusChanged(final String provider, final int status, final Bundle extras)
            {

            }
        };
    }

    public GPSCallback getGPSCallback()
    {
        return GPSManager.gpsCallback;
    }

    public void setGPSCallback(final GPSCallback gpsCallback)
    {
        GPSManager.gpsCallback = gpsCallback;
    }

    public void startListening(final Context context)
    {
        if (GPSManager.locationManager == null)
        {
            GPSManager.locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        }

        final Criteria criteria = new Criteria();

        criteria.setAccuracy(Criteria.ACCURACY_MEDIUM);  // accuracy_midium
        criteria.setSpeedRequired(true);
        criteria.setAltitudeRequired(false);
        criteria.setBearingRequired(true);
        criteria.setCostAllowed(true);
        criteria.setPowerRequirement(Criteria.POWER_MEDIUM);

        final String bestProvider = GPSManager.locationManager.getBestProvider(criteria, true);

        if (bestProvider != null && bestProvider.length() > 0)
        {
            GPSManager.locationManager.requestLocationUpdates(bestProvider, this.gpsMinTime,
                    this.gpsMinDistance, GPSManager.locationListener);

        }
        else
        {
            final List<String> providers = GPSManager.locationManager.getProviders(true);

            for (final String provider : providers)
            {
                GPSManager.locationManager.requestLocationUpdates(provider, this.gpsMinTime,
                        this.gpsMinDistance, GPSManager.locationListener);
            }
        }
    }

    public void stopListening()
    {
        try
        {
            if (GPSManager.locationManager != null && GPSManager.locationListener != null)
            {
                GPSManager.locationManager.removeUpdates(GPSManager.locationListener);
            }

            GPSManager.locationManager = null;
        }
        catch (final Exception ex)
        {

        }
    }


}

我的位置更新代码在这里:

private static final long GPS_MINTIME = 30000;
private static final float GPS_MINDISTANCE = 100;
gpsManager = new GPSManager(GPS_MINTIME,GPS_MINDISTANCE);
                gpsManager.startListening(getApplicationContext());
                gpsManager.setGPSCallback(this);

任何想法为什么会发生这种情况。谢谢。

4

3 回答 3

0

请在 SO https://stackoverflow.com/a/4245527/1438915上查看此答案

在 api < 4.0 min 时间没有强制考虑它有时可能是不准确的。像提示一样工作

更新

来自 docs INFO我认为您应该阅读此内容。

于 2013-02-28T19:20:37.853 回答
0

根据我的评论,这是一个已知的(但未广泛报道的错误/功能)。请参阅Google Groups Post 我确实添加了对此提案的支持,但该线程现已关闭,因为它已在 4.1 中修复

于 2013-02-28T19:47:00.547 回答
0

更改GPS_MINTIME大于6000
它将完美运行。

于 2013-02-28T19:15:45.163 回答