Android Listen For Change in GPS Status?

为君一笑 提交于 2019-12-30 07:46:12

问题


Hello I need to know how to listen for the following with the GPS in Android so I can update the UI in a PreferenceActivity. I have tried GpsStatus.Listener with nothing happening.

  • GpsStatus.GPS_EVENT_STARTED
  • GpsStatus.GPS_EVENT_STOPPED

Any suggestions would be great.


回答1:


    mLocationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

    // Define a listener that responds to location updates
    mGPSStatusListener = new GpsStatus.Listener() {
        public void onGpsStatusChanged(int event) {
            switch (event) {
            case GpsStatus.GPS_EVENT_SATELLITE_STATUS:
                satelliteStatus = mLocationManager.getGpsStatus(null);

                Iterable<GpsSatellite> iSatellites = satelliteStatus
                        .getSatellites();
                Iterator<GpsSatellite> it = iSatellites.iterator();
                maxsatellites = 0;
                while (it.hasNext()) {
                    GpsSatellite oSat = (GpsSatellite) it.next();
                    statArray[maxsatellites][0] = oSat.getPrn();
                    statArray[maxsatellites][1] = oSat.getAzimuth();
                    statArray[maxsatellites][2] = oSat.getPrn();
                    statArray[maxsatellites][3] = oSat.getElevation();
                    statArray[maxsatellites][4] = oSat.getSnr();
                    if (oSat.usedInFix()) {
                        statArray[maxsatellites][5] = 1;
                    } else {
                        statArray[maxsatellites][5] = 0;
                    }
                    maxsatellites++;
                }

                if (mLastLocation != null)
                    if ((SystemClock.elapsedRealtime() - mLastLocationMillis) < 3000) {
                        isGPSFix = 7; // Enumeration for ONC_STAT_3D
                    } else {
                        isGPSFix = 2; // Enumeration for ONC_STAT_BAD_COVER
                    }

                }

                if (isGPSFix == 1) { // A fix has been acquired.
                    // Do something.
                } else { // The fix has been lost.
                    // Do something.
                }

                break;
            case GpsStatus.GPS_EVENT_FIRST_FIX:
                // Do something.
                isGPSFix = 1;
                break;
            case GpsStatus.GPS_EVENT_STOPPED:
                if ((mLastLocation = mLocationManager
                        .getLastKnownLocation(LocationManager.GPS_PROVIDER)) != null) {
                    isGPSFix = 5; // Enumeration for                    } else {
                    isGPSFix = 2; // Enumeration for 
                }

            }
        }

    };

    mGPSLocationListener = new LocationListener() {
        public void onLocationChanged(Location location) {
            // Called when a new location is found by the location
            // provider.
            if (location == null)
                return;

            mLastLocationMillis = SystemClock.elapsedRealtime();

            // Do something.

            mLastLocation = location;
                        }
        }

        public void onProviderDisabled(String provider) {
            // TODO Auto-generated method stub

        }

        public void onProviderEnabled(String provider) {
            // TODO Auto-generated method stub

        }

        public void onStatusChanged(String provider, int status,
                Bundle extras) {
            // TODO Auto-generated method stub

        }
    };

    mLocationManager.addGpsStatusListener(mGPSStatusListener);


    // Register the listener with the Location Manager to receive location
    // updates
    mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
            mUpdateIntervalInMillis, 0, mGPSLocationListener);



回答2:


Looked at the source code for the system settings app and how they did it and implemented it into my app and it works great. @SKJ thx for the help but sadly I couldn't get the GpsStatus.Listener to work the source code works perfectly.



来源:https://stackoverflow.com/questions/8146511/android-listen-for-change-in-gps-status

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!