5

アプリでジオフェンスを使用しています。トリガーされたジオフェンスの削除を除いて、すべて正常に動作します。Android の公式ドキュメントからガイドを削除しましたが、IntentService 内のジオフェンスを削除する方法が説明されていません。

サービスのイベント ハンドラーのコードは次のとおりです。

@Override
protected void onHandleIntent(Intent intent) 
{
    Log.e("GeofenceIntentService", "Location handled");

    if (LocationClient.hasError(intent)) 
    {
        int errorCode = LocationClient.getErrorCode(intent);
        Log.e("GeofenceIntentService", "Location Services error: " + Integer.toString(errorCode));
    } 
    else 
    {
        int transitionType = LocationClient.getGeofenceTransition(intent);
        if (transitionType == Geofence.GEOFENCE_TRANSITION_ENTER)
        {
            List <Geofence> triggerList = LocationClient.getTriggeringGeofences(intent);
            String[] triggerIds = new String[triggerList.size()];

            for (int i = 0; i < triggerIds.length; i++) 
            {
                // Store the Id of each geofence
                triggerIds[i] = triggerList.get(i).getRequestId();

                Picture p = PicturesManager.getById(triggerIds[i], getApplicationContext());
                   /* ... do a lot of work here ... */


            }

        } 
        else 
            Log.e("ReceiveTransitionsIntentService", "Geofence transition error: " + Integer.toString(transitionType));
    }
}

彼がトリガーされた後、どうすればジオフェンスを削除できますか?

4

2 に答える 2

1

ジオフェンスを追加するときと同じように進めます (を作成し、LocationClient接続を待ちます)。接続したら、onConnectedコールバック メソッドで代わりremoveGeofencesLocationClientインスタンスを呼び出し、削除するリクエスト ID のリストと のインスタンスをOnRemoveGeofencesResultListenerコールバック ハンドラとして渡します。

もちろん、GeoFencewithGeoFence.Builderのを作成したときに使用したものと同じリクエスト ID を使用する必要がありますsetRequestId

@Override
public void onConnected(Bundle arg0) {
    locationClient.removeGeofences(requestIDsList, 
    new OnRemoveGeofencesResultListener() {
    ...     
});
于 2014-04-15T02:18:13.007 に答える