4

我有一些服务启动 Google Play 服务位置/活动的代码,当调用 Activity 意图时,我想更改setPriority位置服务的。

// Location
           mLocationRequest = LocationRequest.create();
           if (mostProbActName.equals("still")) {
               mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
               Log.i(TAG, "GOING TO BALANCED MODE!");
           } else {
               mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
               Log.i(TAG, "GOING TO HIGH MODE!");
           }

我将以下内容放在该onHandleIntent部分中,但似乎并没有改变其行为。有没有更好的方法来实现这一点?

4

1 回答 1

0

您必须启动和停止位置客户端才能进行更改。下面的示例代码停止并启动 google play services 融合位置提供程序并重新连接,然后在 onConnected 中使用新设置。位置管理器停止和启动新设置的主体类似。

// only process changes  start is passed in isRecording is global
    if (isRecording != start) {
        isRecording = start;
        if (isRecording) {
            if (locationClient != null && locationClient.isConnected()) {
                locationClient.removeLocationUpdates(locationListener);
                locationClient.disconnect();
            }
            locationClient = null;
            Context context = weakContext.get();
            if (context != null) {
                locationClient = new LocationClient(context,
                        connectionCallbacks,
                        onConnectionFailedListener);
            }
            if (locationClient != null) {
                locationClient.connect();
            }
        } else if (locationClient != null) {
            if (locationClient.isConnected()) {
                locationClient.removeLocationUpdates(locationListener);
                locationClient.disconnect();
            }
            locationClient = null;
        }
    }



@Override
public void onConnected(Bundle arg0) {
    if (locationClient != null) {
        locationClient
                .requestLocationUpdates(
                        LocationRequest
                                .create()
                                .setInterval(locationInterval)
                                .setFastestInterval(locationInterval)
                                .setPriority(
                                        LocationRequest.PRIORITY_HIGH_ACCURACY)
                                .setSmallestDisplacement(recordDistance),
                        locationListener);
        runNotification();
    }
}
于 2015-02-04T15:33:14.507 回答