【问题标题】:Why does Android FusedLocationProviderApi requestLocationUpdates send updates with weird keys?为什么 Android FusedLocationProviderApi requestLocationUpdates 使用奇怪的键发送更新?
【发布时间】:2015-07-09 18:10:11
【问题描述】:

我正在使用 com.google.android.gms:play-services:7.3.0。这在 6.5 中没有发生。

documentation 说:

位置更新使用 KEY_LOCATION_CHANGED 键和意图上的位置值发送。

嗯,这是真的。它还使用密钥发送更新: com.google.android.gms.location.EXTRA_LOCATION_RESULT

以及另一个带有密钥的更新: com.google.android.gms.location.EXTRA_LOCATION_AVAILABILITY

这些是干什么用的?它们记录在哪里?

这是我的代码:

// Needed for all API calls
mClient = new GoogleApiClient.Builder(this)
        .addApi(LocationServices.API)
        .addConnectionCallbacks(this)
        .addOnConnectionFailedListener(this)
        .build();
mClient.connect();

// Create the LocationRequest object
mLocationRequest = LocationRequest.create();
// Use high accuracy
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
// Set the update interval to 5 seconds
mLocationRequest.setInterval(5000);
// Set the fastest update interval
mLocationRequest.setFastestInterval(5000);

Intent locationIntent = new Intent(this, LocationReceiver.class);

@Override
public void onConnected(Bundle connectionHint) {
    Intent locationIntent = new Intent(this, LocationReceiver.class);
    // Set up periodic location updates
    mLocationPendingIntent = PendingIntent.getBroadcast(this, 0, locationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    mFusedLocationProviderApi.requestLocationUpdates(mClient, mLocationRequest, mLocationPendingIntent);
}

然后这是我的 LocationReceiver:

public class LocationReceiver extends BroadcastReceiver {

    private static final String TAG = "LocationReceiver";

    @Override
    public void onReceive(Context context, Intent intent) {
        android.os.Debug.waitForDebugger();  // this line is key
        Log.w(TAG, "received location!");

        Bundle extras = intent.getExtras();

        // Log intent details
        for (String key : extras.keySet()) {
            Object value = extras.get(key);
            Log.d(TAG, String.format("%s %s (%s)", key,
                    value.toString(), value.getClass().getName()));
        }
    }
}

【问题讨论】:

标签: android android-fusedlocation android-googleapiclient


【解决方案1】:

LocationAvailability 提供“位置数据可用性的状态”。

谷歌文档说: “从通过 #requestLocationUpdates(GoogleApiClient,LocationRequest,LocationCallback,Looper) 注册的 LocationCallback 或从通过 #requestLocationUpdates(GoogleApiClient, LocationRequest, PendingIntent) 注册的 PendingIntent 提供。它也可通过 getLocationAvailability(GoogleApiClient) 按需提供)。”

这就是您收到位置可用性数据的原因。

【讨论】:

猜你喜欢
  • 2015-05-11
  • 1970-01-01
  • 2015-08-10
  • 1970-01-01
  • 1970-01-01
  • 2020-07-16
  • 1970-01-01
  • 2016-06-27
  • 1970-01-01
相关资源
最近更新 更多