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

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

    get into similar problem while working on my MSc project, it seems that Daye's answer mistakenly reported "no fix" while the device stays in a static location. I've modified the solution just a little bit which seems to work fine for me in a static location. I don't how would it affect the battery as it is not my main concern, but here's how i did it by re-requesting location updates when a fix has timed out.

    private class MyGPSListener implements GpsStatus.Listener {
        public void onGpsStatusChanged(int event) {
            switch (event) {
            case GpsStatus.GPS_EVENT_SATELLITE_STATUS:
                if (Global.getInstance().currentGPSLocation != null)
                {
                    if((SystemClock.elapsedRealtime() - mLastLocationMillis) < 20000)
                    {
                        if (!hasGPSFix) 
                            Log.i("GPS","Fix Acquired");
                        hasGPSFix = true;
                    } 
                    else
                    {
                        if (hasGPSFix) 
                        {
                            Log.i("GPS","Fix Lost (expired)");
                            lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 10, locationListener);
                        }
                        hasGPSFix = false;
                    }
                }
                break;
            case GpsStatus.GPS_EVENT_FIRST_FIX:
                Log.i("GPS", "First Fix/ Refix");
                hasGPSFix = true;
                break;
            case GpsStatus.GPS_EVENT_STARTED:
                Log.i("GPS", "Started!");
                break;
            case GpsStatus.GPS_EVENT_STOPPED:
                Log.i("GPS", "Stopped");
                break;
            }
        }
    }
    

提交回复
热议问题