GpsStatus.Listener only works when GPS is on

In my application, I have a GpsStatus.Listener to receive events when the user turns GPS on or off. Everything works fine if GPS is turned on before I launch the app . In this case, I get GPS_EVENT_STARTED or GPS_EVENT_STOPPED every time I turn GPS on or off.

The problem is, if the GPS is turned off and the application starts up . In this case, I do not receive an event if I turn GPS on or off.

Can someone explain this to me?

Here is my code:

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; 

    private void setupGPS() {
            locationManager = (LocationManager) getContext().getSystemService(Context.LOCATION_SERVICE);                
            isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
            if(!isGPSEnabled) {
                    Toast.makeText(getContext(), "GPS disabled", Toast.LENGTH_SHORT).show();
            } else {        
                    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME, MIN_DISTANCE, locListener);

    public void onGpsStatusChanged(int event) {
            Log.e("onGpsStatusChanged", event+""); 
            switch(event) {
            case GpsStatus.GPS_EVENT_STARTED:
                    Log.e("onGpsStatusChanged", "GPS_EVENT_STARTED");
            case GpsStatus.GPS_EVENT_STOPPED:
                    Log.e("onGpsStatusChanged", "GPS_EVENT_STOPPED");


So my logcat output (if GPS is enabled at startup):

  • GpsStatusChanged (24638): 1 // application starts
  • GpsStatusChanged (24638): GPS_EVENT_STARTED
  • GpsStatusChanged (24638): 4 // search for fixes
  • GpsStatusChanged (24638): 4
  • GpsStatusChanged (24638): 2 // disable GPS
  • GpsStatusChanged (24638): GPS_EVENT_STOPED

GPS output off: nothing.


source to share

1 answer

Well, LocationListener

provided by:


, onProviderEnabled()

and onStatusChanged()

for this purpose.


provides information about the inner workings of the GPS service. It should not be used to indicate the status of a GPS traffic provider.


provides information on suppliers. The moment you register LocationListener

with the location provider, onProviderEnabled()

/ is called onProviderDisabled()

and your app can always indicate when GPS is on or off.

Try the following:

public class test extends Activity implements LocationListener{

    protected void onCreate(Bundle savedInstanceState) {

        LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

    public void onLocationChanged(Location location) {

    public void onStatusChanged(String s, int i, Bundle bundle) {

    public void onProviderEnabled(String s) {
            Toast.makeText(this,"GPS on",Toast.LENGTH_SHORT).show();

    public void onProviderDisabled(String s) {
            Toast.makeText(this,"GPS off",Toast.LENGTH_SHORT).show();




All Articles