【发布时间】:2016-11-10 03:49:52
【问题描述】:
我只是想听听 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 1st 中运行良好,但已被弃用并且在某些设备中无法运行。
我试图弄清楚如何在这种情况下使用onProviderEnabled()/onProviderDisabled()。我尝试了很多代码示例,但都没有成功。
我正在尝试这个:
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)
【问题讨论】: