我有一些代码,服务会在其中启动Google Play服务位置/活动,当调用“活动”意图时,我想更改位置服务的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部分中添加了以下内容,但似乎并没有改变其行为。有没有更好的方法来实现这一目标?

最佳答案

您必须启动和停止位置客户端才能进行更改。下面的示例代码停止并启动google play服务融合的位置提供程序,然后重新连接,然后在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();
    }
}

关于android - 在Android定位服务中动态更改setPriority,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28185208/

10-10 18:14
查看更多