首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >触发后移除Geofence

触发后移除Geofence
EN

Stack Overflow用户
提问于 2014-04-14 04:24:33
回答 2查看 7.3K关注 0票数 4

我在我的应用程序中使用了geofences,除了移除触发的Geofences之外,一切都很正常。我参考了Android的官方文档中的指南,但他们并没有解释如何在IntentService中删除geofence。

以下是服务的事件处理程序的代码:

代码语言:javascript
复制
@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));
    }
}

在他被触发后,我如何删除他?

EN

回答 2

Stack Overflow用户

发布于 2015-01-30 07:21:15

你可以这样做:

代码语言:javascript
复制
LocationServices.GeofencingApi.removeGeofences(
           mGoogleApiClient,
           // This is the same pending intent that was used in addGeofences().
           getGeofencePendingIntent()
).setResultCallback(this); // Result processed in onResult().

您的getGeofencePendingIntent()方法可能如下所示:

代码语言:javascript
复制
/**
 * 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;
}
票数 2
EN

Stack Overflow用户

发布于 2014-04-15 10:18:13

您可以像添加Geofence时一样继续操作(创建LocationClient并等待其连接)。连接之后,在onConnected回调方法中,您将在LocationClient实例上调用removeGeofences,并向其传递要删除的请求is列表和作为回调处理程序的OnRemoveGeofencesResultListener实例。

当然,您必须使用与使用GeoFence.BuildersetRequestId创建GeoFence时相同的请求ID。

代码语言:javascript
复制
@Override
public void onConnected(Bundle arg0) {
    locationClient.removeGeofences(requestIDsList, 
    new OnRemoveGeofencesResultListener() {
    ...     
});
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23048012

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档