【发布时间】:2018-12-10 22:34:34
【问题描述】:
在我的 Android 应用中,我目前正在使用 Fused Location Provider api(参见下面的代码)来获取我的用户的位置,并且我很少抱怨我的应用无法在应用中获取他们自己的位置。
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(LOCATION_INTERVAL);
mLocationRequest.setFastestInterval(1000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
if ( ContextCompat.checkSelfPermission( this, android.Manifest.permission.ACCESS_FINE_LOCATION ) == PackageManager.PERMISSION_GRANTED ) {
mLocationCallback = new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
if (locationResult == null) {
return;
}
if(locationResult.getLastLocation() != null)
currentLocation = locationResult.getLastLocation();
}
};
mFusedLocationClient.requestLocationUpdates(mLocationRequest, mLocationCallback,null);
locationOn = true;
}
经过一些检查,它似乎有时会在一段时间(或永远)内不断返回空值,而我的应用程序将无法运行。其他 GPS 应用程序也能正常工作(例如 Google 地图)。
我的实现是正确的,还是像 Google Play Services 这样的外部设备被置于省电模式?
【问题讨论】: