我想检测用户何时更改Android手机的GPS设置.当用户打开/关闭GPS卫星或通过接入点等进行检测时的含义
在我的应用程序中,我有一个GpsStatus.Listener,用于在用户启用或禁用GPS时接收事件.如果在启动应用程序之前启用GPS,一切正常.在这种情况下,每次打开或关闭GPS时,我都会收到GPS_EVENT_STARTED或GPS_EVENT_STOPPED.
问题是如果应用程序启动时GPS已关闭.在这种情况下,如果我打开或关闭GPS,我不会收到任何事件.
有人可以向我解释一下吗?
这是我的代码:
public class GPSTracker implements android.location.GpsStatus.Listener {
private final Context context;
private final LocationListener locListener;
private LocationManager locationManager;
private boolean isGPSEnabled = false;
public GPSTracker(Context context, LocationListener locListener) {
this.context = context;
this.locListener = locListener;
setupGPS();
}
private void setupGPS() {
locationManager = (LocationManager) getContext().getSystemService(Context.LOCATION_SERVICE);
locationManager.addGpsStatusListener(this);
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
if(!isGPSEnabled) {
Toast.makeText(getContext(), "GPS disabled", Toast.LENGTH_SHORT).show();
} else {
locationManager.removeUpdates(locListener);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME, MIN_DISTANCE, locListener);
}
}
@Override
public …Run Code Online (Sandbox Code Playgroud)