Android GpsStatus始终拥有零卫星

时间:2016-09-06 17:48:58

标签: java android gps android-location android-gps

我正在尝试获取GPS定位中使用的卫星以获取更多信息,但GpsStatus.getSatellites()方法始终返回空迭代器,即使我可以获得该位置的纬度/经度/高度也不错。

LocationManager locationManager = (LocationManager) this.getSystemService(LOCATION_SERVICE);
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

if (location != null) {

    //get location coordinates
    mLatitude = location.getLatitude();
    mLongitude = location.getLongitude();
    double altitude = location.getAltitude();
    Log.d("test2", "lat : " + mLatitude);
    Log.d("test2", "long : " + mLongitude);

    //get satellite data
    GpsStatus gpsStatus = locationManager.getGpsStatus(null);

    int satellites = 0;
    int satellitesInFix = 0;
    int timetofix = gpsStatus.getTimeToFirstFix();
    Log.d("test2", "Time to first fix = " + timetofix);
    for (GpsSatellite sat : gpsStatus.getSatellites()) {
        if(sat.usedInFix()) {
            satellitesInFix++;
        }
        satellites++;
    }
    Log.d("test2", "Satellites: " + satellitesInFix + "/" + satellites);
}

我在调试日志中的所有内容都是以下内容,一遍又一遍:

09-06 18:42:48.093 6842-6842/com.android.example.gpstest D/test2: lat : 39.509401143731274
09-06 18:42:48.093 6842-6842/com.android.example.gpstest D/test2: long : -9.064780960816815
09-06 18:42:48.094 6842-6842/com.android.example.gpstest D/test2: Time to first fix = 0
09-06 18:42:48.095 6842-6842/com.android.example.gpstest D/test2: Satellites: 0/0

我已经尝试过使用多个设备,认为它可能与特定型号有些怪癖,但它们都有相同的问题,所以看起来我的代码不对。我需要做些什么来获得卫星?

1 个答案:

答案 0 :(得分:2)

因为您尚未启动GPS。 getLastKnownLocation不会打开GPS,它只返回缓存值(如果有)。您需要requestLocationUpdates才能这样做。