7

I am developing an application that I want to learn about the count of gps satellites. I am using the "onSatelliteStatusChanged" method for this, but it does not work. The piece of code that I use below that you see.

Code

 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            gnssStatusCallBack = new GnssStatus.Callback() {
                @Override
                public void onSatelliteStatusChanged(GnssStatus status) {

                        satelliteCount = status.getSatelliteCount();
                }
            };
            locManager.registerGnssStatusCallback(gnssStatusCallBack);
 } else {
      locManager.addGpsStatusListener(this);
 }
}

Note: I tested it outside.

merveylcu
  • 73
  • 1
  • 1
  • 6
  • 1
    Hello, Yes I found the solution. The problem stems from the fact that the super method is not called. Call the super.onSatelliteStatusChanged (status) inside onSatelliteStatusChanged method. – merveylcu May 22 '17 at 14:59
  • Does someone know how many the number should be to say Good GPS Availability? and also Bad GPS Availability? I need it to distinguish between indoor and outdoor location! – Mohsen Emami Dec 08 '22 at 13:55

2 Answers2

13

Overview (request location updates to receive them):

Without explicitly asking for location updates, your device may not be seeking this information. The following worked for me where member mGnssStatusCallback is your GnssStatus.Callback and the Activity (or some other class) implements LocationListener; here this activity implements it and I pass this as the final parameter to requestLocationUpdates and removeUpdates. The 30000 indicates that we're requesting updates every 30 seconds (30000 milliseconds), with 0 indicating that we only want an update if the device has moved > 0 meters.


You probably need to figure out what providers are available, etc.
see Android Training.

Example

public class GnssActivity extends Activity implements LocationListener {
    GnssStatus.Callback mGnssStatusCallback;
    LocationManager mLocationManager;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mLocationManager = 
                (LocationManager) getSystemService(LOCATION_SERVICE);
        mGnssStatusCallback = new GnssStatus.Callback() {
            // TODO: add your code here!
        };

    }

    @Override
    protected void onStart() {
        super.onStart();
        mLocationManager.registerGnssStatusCallback(mGnssStatusCallback);
        mLocationManager.requestLocationUpdates(
                LocationManager.GPS_PROVIDER, 30000, 0, this
        );
    }

    @Override
    protected void onStop() {
        mLocationManager.removeUpdates(this);
        mLocationManager.unregisterGnssStatusCallback(
                mGnssStatusCallback
        );
        super.onStop()
    }

    @Override
    public void onLocationChanged(Location location) {
    }

    @Override
    public void onStatusChanged(String provider, int status, 
            Bundle extras) {
    }

    @Override
    public void onProviderEnabled(String provider) {
    }

    @Override
    public void onProviderDisabled(String provider) {
    }

    ⋮
}
Vlad
  • 7,997
  • 3
  • 56
  • 43
Andrew Siplas
  • 875
  • 12
  • 16
2

Note also that the

GnssStatus Callback

would never get called if the provider is not set to GPS_PROVIDER

locmgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, 30000, 0, this);

A common mistake is to set the provider to NETWORK_PROVIDER

pcodex
  • 1,812
  • 15
  • 16