getTriggeringLocation 总是 returns 空

getTriggeringLocation always returns null

我遇到了 Android 融合位置 API 的问题。我有一个 IntentListener 服务,我需要定期接收位置信息。我成功创建并连接了 GoogleApiClient,然后我请求通过 PendingIntent 接收位置更新,但每次调用 GeofencingEvent.getTriggeringLocation 时,return 值始终为空。

代码如下:

private void createLocationRequest() {
    mLocationRequest = new LocationRequest();
    mLocationRequest.setInterval(INTERVAL);
    mLocationRequest.setFastestInterval(FASTEST_INTERVAL);
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}

private void startLocationListener(Context context) {
    if (!isGooglePlayServicesAvailable(context)) {
        Log.d(TAG, "No Google Play Services.");
        return;
    }

    createLocationRequest();
    mGoogleApiClient = new GoogleApiClient.Builder(context)
            .addApi(LocationServices.API)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .build();

    mGoogleApiClient.connect();
    }

private void startLocationUpdates(Context context) {
    Intent i = new Intent(context, IntentListener.class);
    PendingIntent pi = PendingIntent.getService(context, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);

    com.google.android.gms.common.api.PendingResult<Status> pendingResult = LocationServices.FusedLocationApi.requestLocationUpdates(
            mGoogleApiClient, mLocationRequest, pi);
    Log.d(TAG, "Location update started ..............: ");
}

@Override
protected void onHandleIntent(Intent intent) {
    Log.d(TAG, "INTEEENT");
    String action = intent.getAction();

    if(Actions.ACTION_START_INTENT_LISTENER.equals(action)) {
        init(this);
    }
    else {
        GeofencingEvent event = GeofencingEvent.fromIntent(intent);
        if(event == null) Log.d(TAG, "Null event");
        if(event.getTriggeringLocation() == null) Log.d(TAG, "null location"); // **<-- PROBLEM HERE**
        if (event == null || event.getTriggeringLocation() == null) {
            return;
        }

        Location location = event.getTriggeringLocation();
        Log.d(TAG, "LocationC: " + location.getLatitude() + ", " + location.getLongitude());
    }
}

这是清单

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.srn.app.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <service android:enabled="true" android:name="com.srn.app.IntentListener"></service>
</application>

<meta-data
    android:name="com.google.android.gms.version"
    android:value="@integer/google_play_services_version" />

我是 运行 x86 模拟器中的应用程序,安装了 Android 5.1。

知道可能是什么问题吗?

谢谢。

必须通过 GeofencingApi.addGeofences(). Assuming you actually want to receive the location information from the requestLocationUpdates() call you are doing, you should use LocationResult.extractResult() 代替 GeofencingEvent.fromIntent() 专门注册地理围栏 API。

旧版本 Google Play 服务的替代方法是使用密钥 KEY_LOCATION_CHANGED 从 Intent 中仅提取一个位置:

Location location = intent.getParcelableExtra(
    LocationServices.FusedLocationProviderApi.KEY_LOCATION_CHANGED);