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; 
            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;
            }
    }

      

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.

+3


source to share


1 answer


Well, LocationListener

provided by:

onProviderDisabled()

, onProviderEnabled()

and onStatusChanged()

for this purpose.

GpsStatus.Listener

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



LocationListener

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{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

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


    @Override
    public void onLocationChanged(Location location) {
    }

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

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

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

      

+7


source







All Articles