Would like to listen to gps availability. Followed this. However, onGpsStatusChanged
was not fired even when I disabled/enabled gps. Tried on multiple phones. Also, looked into related stackoverflow Qns such as this, this and this. However met with no success. Any help is appreciated. Thanks!
Code Snippets:
Listener
Listener mGPSListener = new GpsStatus.Listener() {
@Override
public void onGpsStatusChanged(final int event) {
Log.d(TAG, "---onGpsStatusChanged---");
switch (event) {
case GpsStatus.GPS_EVENT_STARTED:
Log.d(TAG, "GPS_EVENT_STARTED");
break;
case GpsStatus.GPS_EVENT_FIRST_FIX:
Log.d(TAG, "GPS_EVENT_FIRST_FIX");
break;
case GpsStatus.GPS_EVENT_STOPPED:
Log.d(TAG, "GPS_EVENT_STOPPED");
break;
case GpsStatus.GPS_EVENT_SATELLITE_STATUS:
Log.d(TAG, "GPS_EVENT_SATELLITE_STATUS");
break;
default:
Log.d(TAG, "SOMETHING_ELSE");
break;
}
}
};
Service
.....
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
Log.d(TAG, "onStartCommand");
Context ctx = getBaseContext();
Log.d(TAG, "Done, " + ctx);
LocationManager locManager = (LocationManager)
ctx.getSystemService(Context.LOCATION_SERVICE);
Log.d(TAG, "Got locManager");
locManager.addGpsStatusListener(mGPSListener);
Log.d(TAG, "Added -> locManager.addGpsStatusListener(mGPSListener)");
return START_STICKY;
}
.....
Could see log statements for context
, LocationManager
, etc. However nothing from onGpsStatusChanged
at all.
Edit1 My manifest contains following permissions:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
OK, so here is my finding.
If you want onGpsStatusChanged
of your GpsStatus.Listener
called you need to NOT ONLY register it to your LocationManager
using addGpsStatusListener
but also listen for location updates by requestLocationUpdates
.
As soon as I started listening to location updates by means of requestLocationUpdates
of LocationManager
class, I started getting gps status changes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With