Since Android API 24 GpsStatus
is deprecated and one should use GnssStatus
. Let us have an activity or a service processing Gps data and a LocationManager
already created.
private GnssStatus.Callback gnssCallback;
public void initCallbacks() {
....
gnssCallback = new GnssStatus.Callback() {
@Override
public void onSatelliteStatusChanged(@NonNull GnssStatus status) {
final int satelliteCount = status.getSatelliteCount();
int usedCount = 0;
for (int i = 0; i < satelliteCount; ++i)
if (status.usedInFix(i))
++usedCount;
Log.d("MyServiceTag", "satellites count = " + satelliteCount + ", used = " + usedCount);
}
};
locationManager.registerGnssStatusCallback(gnssCallback, new Handler(Looper.myLooper()));
....
}
public void deinitCallbacks() {
....
locationManager.unregisterGnssStatusCallback(gnssCallback);
....
}
initCallbacks()
should be called after locationManager
initialization. deinitCallbacks()
should be called when information on the number of satellites is no longer needed, e.g. in onDestroy()
. GnssStatus.getSatelliteCount()
returns total number of known satellites, GnssStatus.usedInFix(int i)
tells whether i-th satellite had been used in the most actual location capture.