我只是想聽聽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編程來說很新,任何人都可以幫助我?