2016-01-28 36 views
3

我有一个LocationReceiver,它使用FusedLocationProviderApi.KEY_LOCATION_CHANGEDIntent中提取Location。但现在KEY_LOCATION_CHANGED已弃用,应该将其更改为?已弃用FusedLocationProviderApi.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 ***"); 
    } 
} 

回答

16

一些研究,我找到了答案后:

@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 
     } 
    } 
} 
+0

你在哪里找到这个答案?我想看看上下文。谢谢! – smfoote

+3

刚刚探讨了[api](https://developers.google.com/android/reference/com/google/android/gms/location/LocationResult)。 – MidasLefko

+0

谢谢!这就是我一直在寻找的。 – smfoote

相关问题