【问题标题】:GPS location updates constantly in Android 2.2 and Android 2.3GPS 位置在 Android 2.2 和 Android 2.3 中不断更新
【发布时间】:2013-03-01 03:47:00
【问题描述】:

我开发了一个应用程序,它每 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);

知道为什么会这样。谢谢。

【问题讨论】:

  • 我在手机上发现了很多相同的东西。当它在 2.3 上时,minUpdateTime 似乎被忽略了,一旦操作系统升级到 ICS,它就会履行合同。
  • 您确定将最小时间设置为30Secs,我看到您在当前代码中已设置为0
  • 很奇怪!我的运行 OS 2.3/2.2 的应用每 2/3 秒闪烁一次以更新 GPS 当前位置。惊讶!
  • @VenomVendor:我在这个类的构造函数中传递值。那是30秒。在 Android 4 - 4.1 版本中已经可以正常工作。有什么想法吗?
  • 尝试将距离从0更改为10

标签: android gps location


【解决方案1】:

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

【讨论】:

  • 我的 GPS_MINTIME 是 30 秒(30000 毫秒),你测试过@VenomVendor。
  • @NickT 代码已经Updated
【解决方案2】:

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

在 api 中 像提示一样工作。

更新

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

【讨论】:

  • 应用程序正在使用 30000 毫秒(30 秒),最小距离是 100 ,所以现在知道了吗?
  • @user748178 对不起,这是我的错误。
  • 感谢您的回复。如果您找到任何解决方案,请在此处分享。
【解决方案3】:

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

【讨论】:

  • 也就是说,我必须上传有这个错误的应用程序?有什么建议吗?
  • 我最终将位置侦听器放在了绑定服务中,并带有自己的调度程序。我将我的最小更新时间作为参数传递给运行由计时器控制的小型状态机的服务。
  • 我还计划在我自己的调度程序/计时器中控制 gps 更新。谢谢@NickT
  • 如果您想支持 Gingerbread 而不是锤击电池,恐怕这是唯一的方法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-03-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多