FusedLocationProviderApi.KEY_LOCATION_CHANGED已被弃用,现在该怎么办?

3

我有一个LocationReceiver,它使用FusedLocationProviderApi.KEY_LOCATION_CHANGED从一个Intent中提取一个Location。但现在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个回答

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
        }
    }
}

你在哪里找到这个答案的?我想看一下上下文。谢谢! - smfoote
3
刚刚探索了API - MidasLefko
谢谢!这就是我在寻找的。 - smfoote
我在 hasResult 中得到了 false。你有什么想法为什么会这样? - Don Box

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接