0

我正在使用 google location api 来获取位置。我面临的问题是,当用户关闭 GPS 时,我没有获得 Lcoation,正如这篇文章所说,即使 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 
   }     
}

所以我的问题是,当位置访问被禁用时是否真的可以访问位置(不是最后缓存的位置),如果是,我的代码有什么问题

4

2 回答 2

1
 if (isNetworkEnabled) {
   manager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME_BTWN_UPDATES,
                            DISTANCE_OF_UPDAPTES, this);
     if (manager != null) {
     location = manager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
       if (location != null) {
          latitude = location.getLatitude();
          longitude = location.getLongitude();
       }
    }
}
于 2016-03-02T04:35:35.577 回答
0

您无法仅使用 Google Api Location 本地启动服务,您应该使用布尔位置管理器根据用户请求管理位置。否则,您的尝试看起来并不肤浅,您可以在 gps 开启或关闭时使用get ,具体取决于检测时间。

于 2016-03-02T05:03:44.837 回答