我有LocationReceiver
使用FusedLocationProviderApi.KEY_LOCATION_CHANGED
从Location
中提取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 ***");
}
}
答案 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
}
}
}