How can I check the current status of the GPS receiver?

前端 未结 17 1864
时光取名叫无心
时光取名叫无心 2020-11-22 08:53

How can I check the current status of the GPS receiver? I already checked the LocationListener onStatusChanged method but somehow it seems that is not working,

17条回答
  •  一向
    一向 (楼主)
    2020-11-22 09:37

    Ok, so let's try a combination of all the answers and updates so far and do something like this:

    • Add the ACCESS_FINE_LOCATION permission to your manifest
    • Get an instance of the system LocationManager
    • Create a GpsStatus.Listener that reacts to GPS_EVENT_SATELLITE_STATUS
    • Register the listener with LocationManager with addGpsStatusListener

    The GPS listener could be something like this:

    GpsStatus.Listener listener = new GpsStatus.Listener() {
        void onGpsStatusChanged(int event) {
            if (event == GPS_EVENT_SATELLITE_STATUS) {
                GpsStatus status = mLocManager.getGpsStatus(null);
                Iterable sats = status.getSatellites();
                // Check number of satellites in list to determine fix state
            }
        }
    }
    

    The APIs are a bit unclear about when and what GPS and satellite information is given, but I think an idea would be to look at how many satellites are available. If it's below three, then you can't have a fix. If it's more, then you should have a fix.

    Trial and error is probably the way to go to determine how often Android reports satellite info, and what info each GpsSatellite object contains.

提交回复
热议问题