触发后移除地理围栏

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

如何在围栏被触发之后删除它?
2个回答

2
您可以像这样做:

您可以这样做:

LocationServices.GeofencingApi.removeGeofences(
           mGoogleApiClient,
           // This is the same pending intent that was used in addGeofences().
           getGeofencePendingIntent()
).setResultCallback(this); // Result processed in onResult().

你的getGeofencePendingIntent()方法可以像这样:

/**
 * Gets a PendingIntent to send with the request to add or remove Geofences. Location Services
 * issues the Intent inside this PendingIntent whenever a geofence transition occurs for the
 * current list of geofences.
 *
 * @return A PendingIntent for the IntentService that handles geofence transitions.
 */
private PendingIntent getGeofencePendingIntent() {
    Log.d(TAG, "getGeofencePendingIntent()");
    // Reuse the PendingIntent if we already have it.
    if (mGeofencePendingIntent != null) {
        return mGeofencePendingIntent;
    }
    Intent intent = new Intent(mContext, GeofenceIntentServiceStub.class);
    // We use FLAG_UPDATE_CURRENT so that we get the same pending intent back when calling
    // addGeofences() and removeGeofences().
    mGeofencePendingIntent = PendingIntent.getService(mContext, 0, intent,
            PendingIntent.FLAG_UPDATE_CURRENT);
    return mGeofencePendingIntent;
}

1
您需要按照添加地理围栏时的步骤进行操作(创建一个LocationClient并等待其连接)。一旦它连接成功,在onConnected回调方法中,您将调用LocationClient实例上的removeGeofences方法,并传递要删除的请求ID列表和作为回调处理程序的OnRemoveGeofencesResultListener实例。

当然,您必须使用与使用GeoFence.BuildersetRequestId创建GeoFence时相同的请求ID。
@Override
public void onConnected(Bundle arg0) {
    locationClient.removeGeofences(requestIDsList, 
    new OnRemoveGeofencesResultListener() {
    ...     
});

那我是在服务中创建一个 LocationClient 吗? - Gp2mv3
当添加地理围栏时,我不知道你在哪里创建它。 - Jeshurun
2
LocationClient不再包含在play服务中。 - mrroboaat
在同一地理围栏退出时移除。您必须使用LocationServices.GeofencingApi.removeGeofences(mGoogleApiClient, addGeofences().getGeofencePendingIntent()).setResultCallback(this)。 - Ravin

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接