2011-04-20 94 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中的值來自我的應用程序,因此您應該適應它們。 這段代碼對我來說就像是一種魅力。我知道我有點晚,但我無法在任何地方找到這種方法,並且可以幫助某人。 乾杯