Android 监听 GPS 状态的变化?

发布于 2024-12-15 23:48:32 字数 265 浏览 2 评论 0原文

您好,我需要知道如何在 Android 中使用 GPS 监听以下信息,以便我可以在 PreferenceActivity 中更新 UI。我尝试过 GpsStatus.Listener 但没有任何反应。

  • GpsStatus.GPS_EVENT_STARTED
  • GpsStatus.GPS_EVENT_STOPPED

任何建议都会很好。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜未央樱花落 2024-12-22 23:48:32
    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);
    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);
怪我入戏太深 2024-12-22 23:48:32

查看了系统设置应用程序的源代码以及他们如何做到这一点并将其实现到我的应用程序中,效果很好。感谢 @SKJ 的帮助,但遗憾的是我无法让 GpsStatus.Listener 工作,源代码完美工作。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文