java - Android GpsStatus 始终有零颗卫星

标签 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

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

最佳答案

因为您还没有启动GPS。 getLastKnownLocation 不会打开 GPS,它仅返回缓存值(如果有)。您需要请求LocationUpdates 才能执行此操作。

关于java - Android GpsStatus 始终有零颗卫星,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39355024/

相关文章:

android - 我怎么可能得到这份崩溃报告?我的应用想打电话?

java - findviewbyid 在对话框中返回 null

ios - 如何使 MKAnnotations 在 map 上平滑移动(调整其坐标)?

java - 将对象转换到列表会导致未检查的转换警告

javascript - 内容安全策略:拒绝执行内联脚本

android - 我的 android 应用程序无法从 python 接收 fcm 消息

java - 如何知道一个 jar 文件是否已经在运行?

java - Apache Thrift 中的通用对象

java - 在 Java 中检查属性是否为空的最简单方法

gps - 给定所有纬度/经度坐标,如何确定点是否在矩形内?