2

我正在使用下面的代码监控地理围栏转换

LocationServices.GeofencingApi
    .addGeofences(AssistApplication.getApiHelper().getClient(),
                  getGeofencingRequest(),
                  getPendingIntent(context,  
                              GeofenceTransitionIntentService.class))
    .setResultCallback(this);

这就是我构建 GeofencingRequest 的方式

private GeofencingRequest getGeofencingRequest()
{
    return new GeofencingRequest.Builder()
            .setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER |
                    GeofencingRequest.INITIAL_TRIGGER_EXIT)
            .addGeofence(getGeofence())
            .build();
}

private Geofence getGeofence()
{
    return new Geofence.Builder()
            .setRequestId(requestId)
            .setCircularRegion(latitude, longitude, 100)
            .setExpirationDuration(Geofence.NEVER_EXPIRE)
            .setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER |
                    Geofence.GEOFENCE_TRANSITION_EXIT)
            .build();
}

地理围栏在进入和退出时正确触发,但是当我使用 时getGeofenceTransition(),我没有得到三个GEOFENCE_TRANSITION_标志中的任何一个。

protected void onHandleIntent(Intent intent)
{
    final GeofencingEvent event = GeofencingEvent.fromIntent(intent);
    if (event.hasError())
    {
        Log.e(TAG, getErrorMessage(event.getErrorCode()));
        return;
    }

    switch (event.getGeofenceTransition())
    {
        case Geofence.GEOFENCE_TRANSITION_EXIT:
            // Not triggered 
            break;
        case Geofence.GEOFENCE_TRANSITION_ENTER:
        case Geofence.GEOFENCE_TRANSITION_DWELL:
            // Not triggered
            break;
        default:
            // Triggered on exit and enter
    }
}

请告知我在这里缺少的东西

4

2 回答 2

2

从问题中的信息来看,我看不出您的代码有任何具体问题,但我怀疑Intent您正在处理的不是来自转换警报?

就我个人而言,我使用广播接收器Intent从地理围栏接收,并IntentFilter用于过滤它。我使用的结构如下:-

在你的声明广播接收器Activity

private BroadcastReceiver passedGeofenceReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            int geofenceTransition;

            // get the Geofencing Event
            GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent);
            if (geofencingEvent.hasError()) {
                return;
            }

            // Get the transition type.
            geofenceTransition = geofencingEvent.getGeofenceTransition();

            if ( geofenceTransition == Geofence.GEOFENCE_TRANSITION_ENTER) {
                // etc etc 

在您的方法中注册此接收器Activity onCreate:-

    // register this Activity as the receiver of Geofence notifications
    IntentFilter filter = new IntentFilter();
    filter.addAction("com.example.HIT_GEOFENCE");
    this.registerReceiver(this.passedGeofenceReceiver, filter);

在创建广播PendingIntent时,设置过滤器(mGeofencePendingIntent 是一个成员PendingIntent变量):-

private PendingIntent getGeofencePendingIntent() {
    // Reuse the PendingIntent if we already have it.
    if (mGeofencePendingIntent != null) {
        return mGeofencePendingIntent;
    }

    Intent hitGeofenceIntent = new Intent("com.example.HIT_GEOFENCE"); //- intent to send a broadcast

    mGeofencePendingIntent = PendingIntent.getBroadcast(this, 0, hitGeofenceIntent,  PendingIntent.FLAG_UPDATE_CURRENT);
    return mGeofencePendingIntent;
}

监控您的地理围栏:-

private void monitorGeofences() {
    if ( <check permissions> ) {
        GeofencingRequest geofencingRequest = getGeofencingRequest();
        PendingIntent pendingIntent = getGeofencePendingIntent();
        LocationServices.GeofencingApi.addGeofences(mGoogleApiClient, geofencingRequest, pendingIntent)
                        .setResultCallback(this); // Result callback fires 'onResult'
    }
}

我希望这有帮助。

于 2017-01-08T12:57:06.043 回答
0

我在 Android 12 上使用地理围栏时遇到了同样的问题。没有转换 (-1),没有触发位置,也没有触发意图中的地理围栏。

那是因为我在创建未决意图时使用PendingIntent.FLAG_IMMUTABLE而不是。PendingIntent.FLAG_MUTABLE

因此,在广播意图的情况下应该像这样创建它:

PendingIntent.getBroadcast(
    context, 0,
    Intent(context, GeofenceBroadcastReceiver::class.java),
    PendingIntent.FLAG_UPDATE_CURRENT or
        (if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) PendingIntent.FLAG_MUTABLE else 0)
)
于 2022-02-11T10:34:27.787 回答