【问题标题】:How to time out GPS signal acquisitionGPS信号采集如何超时
【发布时间】:2011-08-09 04:20:32
【问题描述】:

我的应用程序使用 LocationListener 来获取一个位置修复,然后每分钟使用一次 removeUpdates()(以节省电池电量)。问题是,如果用户移动到内部,那么它将永远寻找信号,从而消耗更多电池。我想知道是否有人知道位置管理器中的公共方法,如果在 15 秒内未获取信号,我可以使用它来删除更新。然后我会每五分钟检查一次定位,直到用户回到户外。这有意义吗?也许有更好的方法来超时 GPS 信号采集?我查看了位置管理器的公共方法,但似乎找不到方法。非常感谢你的帮助! -Dom

【问题讨论】:

标签: android gps location android-location


【解决方案1】:

虽然 GPSmaster 的回答被接受,但我想将链接发布到更优雅和更简单的解决方案,我认为 - https://gist.github.com/777790/86b405debd6e3915bfcd8885c2ee11db2b96e3df。我自己试过了,效果很好=)

如果链接失效,这是 amay077 自定义的LocationListener

/**
 * Initialize instance.
 *
 * @param locaMan the base of LocationManager, can't set null.
 * @param timeOutMS timeout elapsed (mili seconds)
 * @param timeoutListener if timeout, call onTimeouted method of this.
 */
public TimeoutableLocationListener(LocationManager locaMan, long timeOutMS,
        final TimeoutLisener timeoutListener) {
    this.locaMan  = locaMan;
    timerTimeout.schedule(new TimerTask() {

        @Override
        public void run() {
            if (timeoutListener != null) {
                timeoutListener.onTimeouted(TimeoutableLocationListener.this);
            }
            stopLocationUpdateAndTimer();
        }
    }, timeOutMS);
}

/***
 * Location callback.
 *
 * If override on your concrete class, must call base.onLocation().
 */
@Override
public void onLocationChanged(Location location) {
    stopLocationUpdateAndTimer();
}

@Override
public void onProviderDisabled(String s) { }

@Override
public void onProviderEnabled(String s) { }

@Override
public void onStatusChanged(String s, int i, Bundle bundle) { }

private void stopLocationUpdateAndTimer() {
    locaMan.removeUpdates(this);

    timerTimeout.cancel();
    timerTimeout.purge();
    timerTimeout = null;
}

public interface TimeoutLisener {
    void onTimeouted(LocationListener sender);
}

【讨论】:

  • 关于如何使用它的任何提示,我有一个实现 LocationListener 的活动?但是如果我让它实现 TimeoutableLocationListener 我会收到一个错误“此处应有接口”?
【解决方案2】:

所以这就是我最终要做的。

这是我添加到 LocationListener 中的,注意全局变量 timertest 和 loccounter:

public class MyLocationListener implements LocationListener

{
    public void onStart() {
        if (timertest==false) {
            timertest=true;
            serviceHandler = new Handler();
            serviceHandler.postDelayed( new timer(),1000L );
        }
    }
    class timer implements Runnable {
          public void run() {
            ++loccounter;
            if (runtest==true && loccounter>8) {
                dt=200;
                runtest=false;
                stoplistening();
            } else serviceHandler.postDelayed( this, 1000L );
          }
    }
}

我在请求位置更新之前启动计时器。

public void locupdate(int minTime, float minDistance) {
    mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    mlocListener = new MyLocationListener();
    if (mlocListener != null && mlocManager != null) {
        mlocManager.removeUpdates(mlocListener);
    }
    loccounter=0;
    ((MyLocationListener) mlocListener).onStart();
    runtest=true;
    mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
                minTime, minDistance, mlocListener);
}

位置监听器中的最后一个方法:

    public void stoplistening() {
        mlocManager.removeUpdates(mlocListener);
        loccounter=0;
    }

希望这对某人有所帮助

【讨论】:

    【解决方案3】:

    嗯,我对这个问题的解决方案有点非常简单,我不知道这是否是好的做法。 假设我们有一个名为 timeToQuitlong,它决定了在中止搜索刚刚放置的位置之前要等待多少毫秒:

    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 
                                  2000, 0, locationListener);
    Handler handler = new Handler();    // android.os.Handler
    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            locationManager.removeUpdates(locationListener);
        }
    };
    handler.postDelayed(runnable, timeToQuit);
    

    当然你必须有一个 LocationListener 和你自己的 LocationListener。 locationManager.requestLocationUpdates 中的值来自我的应用程序,因此您应该调整它们。 这段代码对我来说就像一个魅力。我知道我来晚了,但是我在任何地方都找不到这种方法,并且可能对某人有所帮助。 干杯

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多