我正在使用google位置api獲取位置。我正面臨的問題是,當用戶關閉GPS時,我沒有收到Lcoation,根據this帖子,我們可以訪問基於網絡的位置 - 儘管GPS已關閉。但是當用戶關閉位置訪問權限時,我並未獲得位置信息。當位置訪問關閉時,無法訪問基於網絡的位置
這是我的服務來獲取位置
public class LocationUpdateService extends Service implements GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener, LocationListener, ResultCallback<Status> {
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
startLocationUpdates();
return START_STICKY;
}
@Override
public void onCreate() {
super.onCreate();
LogUtils.LOGI(TAG, "Service created");
buildGoogleApiClient();
setupBroadCastListenerForGpsStatusChange();
initLastLocationDetails();
}
/**
* Requests location updates from the FusedLocationApi.
*/
protected void startLocationUpdates() {
if (!mGoogleApiClient.isConnected()) {
mGoogleApiClient.connect();
} else {
mRequestingLocationUpdates = true;
LocationServices.FusedLocationApi.requestLocationUpdates(
mGoogleApiClient, mLocationRequest, this);
}
}
/**
* Builds a GoogleApiClient. Uses the {@code #addApi} method to request the
* LocationServices API.
*/
protected synchronized void buildGoogleApiClient() {
LogUtils.LOGI(TAG, "Building GoogleApiClient");
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.addApi(ActivityRecognition.API)
.build();
createLocationRequest();
}
protected void createLocationRequest() {
mLocationRequest = new LocationRequest();
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mLocationRequest.setInterval(UPDATE_INTERVAL_IN_MILLISECONDS);
mLocationRequest.setFastestInterval(FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS);
mLocationRequest.setSmallestDisplacement(SMALLEST_DISTANCE_METERS);
}
@Override
public void onConnected(Bundle connectionHint) {
startLocationUpdates();
}
@Override
public void onLocationChanged(Location location) {
//location operations
}
}
所以我的問題是,難道真的有可能當位置的訪問被禁止訪問的位置(不會持續緩存的位置),如果是,是什麼錯我的代碼
請在回答之前仔細閱讀此問題,請參閱我正在使用谷歌播放位置api,併發布Android本地位置api的答案 – droidev