【问题标题】:Android Listen For Change in GPS Status?Android 监听 GPS 状态的变化?
【发布时间】:2011-11-16 03:58:18
【问题描述】:

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

  • GpsStatus.GPS_EVENT_STARTED
  • GpsStatus.GPS_EVENT_STOPPED

任何建议都会很棒。

【问题讨论】:

    标签: android gps preferenceactivity


    【解决方案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);
    

    【讨论】:

    • 在做mLocationManager.addGpsStatusListener(mGPSStatusListener);时应该在onCreateonResume中完成还是两者都可以完成?
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2017-05-31
      • 2015-05-23
      • 1970-01-01
      • 2017-08-08
      • 2019-08-10
      • 1970-01-01
      • 1970-01-01
      • 2021-02-17
      • 1970-01-01
      相关资源
      最近更新 更多