【问题标题】:Dynamically Change setPriority in Android Location Services在 Android 位置服务中动态更改 setPriority
【发布时间】:2015-03-26 22:29:29
【问题描述】:

我有一些代码,服务启动 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 部分,但它似乎并没有改变它的行为。有没有更好的方法来实现这一点?

【问题讨论】:

  • 所以没有设置新的位置请求优先级?我假设您正在清除旧请求并为 FusedLocationApi 提供这个新请求。
  • 发布更多代码,因为我们不会猜到你在哪里做什么

标签: android google-play-services fusedlocationproviderapi


【解决方案1】:

您必须启动和停止位置客户端才能进行更改。下面的示例代码停止并启动 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();
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-15
    • 1970-01-01
    • 2013-03-13
    • 2015-03-07
    • 1970-01-01
    • 2011-09-30
    • 2017-01-24
    相关资源
    最近更新 更多