2016-11-10 127 views
1

我只是想聽聽GPS狀態變化......我有以下幾點:GPS狀態監聽

LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 

    if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
     return; 
    } 

    lm.addGpsStatusListener(new android.location.GpsStatus.Listener() { 

     public void onGpsStatusChanged(int event) { 
      locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
      switch (event) { 
       case GPS_EVENT_STARTED: 
        if((alertDialog != null) && (alertDialog.isShowing())){ 
         alertDialog.dismiss(); 
        } 
        contador=0; 
        darHandle(); 
        break; 
       case GPS_EVENT_STOPPED: 
        if(mProgressDialog!=null && mProgressDialog.isShowing()) { 
         mProgressDialog.dismiss(); 
        } 
        contador=0; 
        break; 
      } 
     } 
    }); 

它可以很好地在我的Moto G的第一次,但它已被棄用,不工作在一些設備中...

我試圖找出如何使用「onProviderEnabled /無效」在那種情況下......我已經嘗試了許多代碼示例,但並沒有得到它的工作...

我正在試試這個:

LocationListener locationListenerGps = new LocationListener() { 
     public void onLocationChanged(Location location) { 

     } 
     public void onProviderDisabled(String provider) {} 
     public void onProviderEnabled(String provider) {} 
     public void onStatusChanged(String provider, int status, Bundle extras) {} 
    }; 

    LocationManager locationManager = (LocationManager) this 
      .getSystemService(Context.LOCATION_SERVICE); 

    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 12000, 0, locationListenerGps); 

但在最後一行,它說:

無法解析法 'requestLocationUpdates(java.lang.String中,INT,INT,com.google.android.gms.location.LocationListener)'

我對Android/Java編程來說很新,任何人都可以幫助我?

回答

0

這只是發生在我身上。

我認爲你的問題必須與導入的監聽器的類有關。

有兩種:

一個你使用:com.google.android.gms.location.LocationListener

和:android.location.LocationListener

您使用屬於第二個這些方法。 使用那個,你應該會好起來的。

在Android中通常會發生這樣的事情,所以要警惕,對於不同的API有很多類,其中一些重複名稱,你必須嘗試組合。