2011-04-20 40 views
9

我的应用程序使用LocationListener获取一个位置修正,然后每分钟一次removeUpdates()(为了节省电池)。问题是,如果用户在里面移动,它会永久寻找信号,燃烧更多的电池。我想知道是否有人知道位置管理器中的公共方法,如果在15秒内未获取信号,我可以使用它来移除更新。然后我会每五分钟检查一次位置修复,直到用户移回户外。这有意义吗?也许有更好的方法来暂停GPS信号采集?我已经浏览了位置管理器的公共方法,但我似乎无法找到实现它的方法。非常感谢你的帮助! -Dom如何超时GPS信号采集

+1

以下链接可以帮助你。 http://stackoverflow.com/questions/2021176/android-gps-status/3712727#3712727 – Siddiqui 2011-04-20 04:47:50

+0

不是我期待的简单答案,但感谢信息! – GPSmaster 2011-04-20 06:00:35

回答

8

虽然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); 
} 
+0

关于如何使用这个任何提示,我有一个实现LocationListener的活动?但如果我让它实现TimeoutableLocationListener我得到一个错误“接口预计在这里”? – Hakim 2016-06-28 05:54:19

4

所以这就是我最终做的。

这是我加入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; 
    } 

希望这可以帮助别人

0

那么我解决这个问题是有些很简单我不知道这是否是好的做法。 假设我们有一个名为longtimeToQuit决定你有多少米利斯想中止只是把搜索位置之前等待:

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中的值来自我的应用程序,因此您应该适应它们。 这段代码对我来说就像是一种魅力。我知道我有点晚,但我无法在任何地方找到这种方法,并且可以帮助某人。 干杯