2010-01-06 68 views
7

通過以下代碼啓動偵聽器後工作正常。android如何停止gps

LocationManager locationManager =(LocationManager) getSystemService(Context.LOCATION_SERVICE); 
locationManager.requestLocationUpdates(
        LocationManager.GPS_PROVIDER, WLConstants.DELAY_HOUR, gpsl 
          .getMinDistance(), gpsl); 

一段時間後,我用下面的代碼

locationManager.removeUpdates(this); 

停止監聽,但問題是,它仍然在尋找我的GPS的任何解決方案???如何阻止gps?

+2

記住接受你以前提出的問題的答案 - 迄今爲止你只有45%的接受度。這樣更多的人可能會繼續回答問題。 :) – 2010-01-06 14:51:38

回答

19

您需要將實現LocationListener的同一對象實現爲locationManager.removeUpdates()方法所請求的位置更新。

所以除非thisgpsl是同一個,你應該叫:

locationManager.removeUpdates(gpsl); 
0

我的聽衆FPGA實現

public class GPSLocationListener extends Activity implements LocationListener { 

    @Override 
     public void onLocationChanged(Location location) { 
//code here 
    } 

    } 

當我嘗試使用follwing代碼編譯時錯誤刪除監聽器。

locationManager.removeGpsStatusListener(GPSLocationListener) 
+2

刪除偵聽器時,您需要使用* instance *名稱而不是* class *名稱。 在您的原始問題中,您的偵聽器是一個名爲'gpsl'的變量。據推測,這是你班上的一個成員變量。如果不是,請將其設爲成員變量而不是該方法的本地變量。然後當你調用removeGpsStatusListener時你可以傳遞'gpsl'。 – 2010-01-06 14:50:08

+1

您不能將removeGpsStatusListener與LocationListener一起使用! LocationListener與GpsStatus.Listener不同。 – Erwan 2013-05-29 02:51:35

0

你必須實例傳遞給你的GPSLocationListenerremoveGpsStatusListener方法,而不是類名。

1

你可以傳遞參數這樣GPSLocationListener.this

locationManager.removeGpsStatusListener(GPSLocationListener.this); 

編譯不會有錯

3

如果您正在使用的地圖,你要做的:

locationManager.removeUpdates(locationListener); 

mMap.setMylocationEnabled(false); 
+0

.setMyLocationEnabled(false)實際上是我的代碼中缺少的。非常感謝! – marson 2014-10-02 16:07:47