不推荐使用FusedLocationProviderApi.KEY_LOCATION_CHANGED。现在做什么?

时间:2016-01-28 09:21:26

标签: android location fusedlocationproviderapi

我有LocationReceiver使用FusedLocationProviderApi.KEY_LOCATION_CHANGEDLocation中提取Intent。但是现在KEY_LOCATION_CHANGED已被弃用,我该将其更改为什么?

当前代码:

@Override
public void onReceive(Context context, Intent intent) {

    final Location location = (Location) intent.getExtras().get(FusedLocationProviderApi.KEY_LOCATION_CHANGED);

    if (location != null) {
        float accuracy = location.getAccuracy();
        Log.d(LocationReceiver.class.getSimpleName(), "*** Accuracy is: " + accuracy + " ***");
    } else {
        Log.d(LocationReceiver.class.getSimpleName(), "*** location object is null ***");
    }
}

1 个答案:

答案 0 :(得分:17)

经过一番研究后我找到了答案:

@Override
public void onReceive(Context context, Intent intent) {

    if (LocationResult.hasResult(intent)) {
        LocationResult locationResult = LocationResult.extractResult(intent);
        Location location = locationResult.getLastLocation();
        if (location != null) {
            // use the Location
        }
    }
}