位置访问关闭时无法访问基于网络的位置

时间:2016-03-02 04:18:44

标签: android gps location fusedlocationproviderapi

我使用谷歌位置api获取位置。我面临的问题是,当用户关闭GPS时,我没有获得Lcoation,因为this帖子说我们甚至可以访问基于网络的位置 - 尽管GPS已关闭。但是当用户关闭位置访问权限时,我没有得到locaton。

这是我获取位置的服务

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 
   }     
}

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

2 个答案:

答案 0 :(得分: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();
       }
    }
}

答案 1 :(得分:0)

您只需使用Google Api位置即可本机启动服务,您应该设法使用布尔位置管理器根据用户请求操作位置。否则你的尝试看起来不肤浅,你可以使用获取 gps 关闭,具体取决于检测到的情况