O GpsStatus.Listener funciona somente se o GPS estiver ativado
no meu aplicativo eu tenho umGpsStatus.Listener para receber eventos quando o usuário habilita ou desabilita o GPS. Tudo funciona bem seGPS está ligado antes de iniciar o aplicativo. Neste caso eu recebo umGPS_EVENT_STARTED ou umGPS_EVENT_STOPPED toda vez que eu ligar ou desligar o GPS.
O problema é seO GPS está desligado enquanto o aplicativo está sendo iniciado. Nesse caso, não receberei um evento se eu ativar ou desativar o GPS.
Alguém pode explicar isso para mim?
Aqui está o meu código:
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 void onGpsStatusChanged(int event) {
Log.e("onGpsStatusChanged", event+"");
switch(event) {
case GpsStatus.GPS_EVENT_STARTED:
Log.e("onGpsStatusChanged", "GPS_EVENT_STARTED");
break;
case GpsStatus.GPS_EVENT_STOPPED:
Log.e("onGpsStatusChanged", "GPS_EVENT_STOPPED");
break;
}
}
Então, minha saída do logcat é (se o GPS estiver ligado ao iniciar):
GpsStatusChanged (24638): 1 // o aplicativo é iniciadoGpsStatusChanged (24638): GPS_EVENT_STARTEDGpsStatusChanged (24638): 4 // procurando por correçõesGpsStatusChanged (24638): 4GpsStatusChanged (24638): 2 // desativar o GPSGpsStatusChanged (24638): GPS_EVENT_STOPEDSaída com GPS desligado: nada.