Skip to content Skip to sidebar Skip to footer

Gpsstatus.listener Works Only If Gps Is On

in my App I have a GpsStatus.Listener to receive events when the user enables or disables GPS. Everything works fine if GPS is on before I start the app. In this case I receive a G

Solution 1:

Well, LocationListener has been provided with:

onProviderDisabled(),onProviderEnabled() and onStatusChanged() for exactly this purpose.

GpsStatus.Listener delivers info about GPS service's inner workings. It is not to be used for telling the status pf GPS Provider.

LocationListener delivers info about providers. The moment you register LocationListener with location provider, onProviderEnabled()/onProviderDisabled() is called accordingly, and your app can always tell when GPS is turned on or off.

Try this:

publicclasstestextendsActivityimplementsLocationListener{

    @OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,1000,10,this);
    }


    @OverridepublicvoidonLocationChanged(Location location) {
    }

    @OverridepublicvoidonStatusChanged(String s, int i, Bundle bundle) {
    }

    @OverridepublicvoidonProviderEnabled(String s) {
        if(LocationManager.GPS_PROVIDER.equals(s)){
            Toast.makeText(this,"GPS on",Toast.LENGTH_SHORT).show();
        }
    }

    @OverridepublicvoidonProviderDisabled(String s) {
        if(LocationManager.GPS_PROVIDER.equals(s)){
            Toast.makeText(this,"GPS off",Toast.LENGTH_SHORT).show();
        }
    }
}

Post a Comment for "Gpsstatus.listener Works Only If Gps Is On"