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();
}

ジオフェンスは、開始時と終了時に正しくトリガーされますが、 を使用すると、3 つのフラグ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 に答える