【发布时间】:2016-05-26 17:41:06
【问题描述】:
案例:应用程序需要设备位置来指定网络请求。如果位置提供程序被禁用,它应该使用最后一个已知位置。作为android.locationseems to be deprecated in favor of Fused location API,我决定使用新的API,但遇到了一些问题。使用旧 API,我的案例看起来像这样:
//get Location
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
...
bestProvider = locationManager.getBestProvider(criteria, true);
protected void onResume() {
super.onResume();
//check providers. If providers disabled, use last known location
if (locationManager.isProviderEnabled(bestProvider)) {
locationManager.requestLocationUpdates(bestProvider, 1000 * 60, 100, this);
} else {
Location location = locationManager.getLastKnownLocation(bestProvider);
}
但我不确定如何在 Fused API 中进行类似操作。新 API 中提供者的可用性如何?因为 Fused Location(GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener) 的新接口没有给我提示。
我想了解的是我的帖子中描述的用例。 如果位置提供程序被禁用,它应该使用最后一个已知位置。因此,核心位置 API 中针对这种情况的条件语句是 isProviderEnabled() 方法的结果。但是新的 API 呢?
【问题讨论】: