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

前端 未结 17 1591
时光取名叫无心
时光取名叫无心 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:28

    You say that you already tried onStatusChanged(), but that does work for me.

    Here's the method I use (I let the class itself handle the onStatusChanged):

    private void startLocationTracking() {
        final int updateTime = 2000; // ms
        final int updateDistance = 10; // meter
        final Criteria criteria = new Criteria();
        criteria.setCostAllowed(false);
        criteria.setAccuracy(Criteria.ACCURACY_FINE);
        final String p = locationManager.getBestProvider(criteria, true);
        locationManager.requestLocationUpdates(p, updateTime, updateDistance,
                this);
    }
    

    And I handle the onStatusChanged as follows:

    void onStatusChanged(final String provider, final int status,
            final Bundle extras) {
        switch (status) {
        case LocationProvider.OUT_OF_SERVICE:
            if (location == null || location.getProvider().equals(provider)) {
                statusString = "No Service";
                location = null;
            }
            break;
        case LocationProvider.TEMPORARILY_UNAVAILABLE:
            if (location == null || location.getProvider().equals(provider)) {
                statusString = "no fix";
            }
            break;
        case LocationProvider.AVAILABLE:
            statusString = "fix";
            break;
        }
    }
    

    Note that the onProvider{Dis,En}abled() methods are about enabling and disabling GPS tracking by the user; not what you're looking for.

提交回复
热议问题