ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Android收听GPS状态的变化?

2019-10-07 07:34:28  阅读:219  来源: 互联网

标签:preferenceactivity android gps


您好我需要知道如何使用Android中的GPS监听以下内容,以便我可以在PreferenceActivity中更新UI.我没有尝试过GpsStatus.Listener.

> GpsStatus.GPS_EVENT_STARTED
> GpsStatus.GPS_EVENT_STOPPED

任何建议都会很棒.

解决方法:

    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);

标签:preferenceactivity,android,gps
来源: https://codeday.me/bug/20191007/1865632.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有