【问题标题】:Remove Geofence after triggered触发后删除地理围栏
【发布时间】:2014-04-13 20:24:32
【问题描述】:

我在我的应用程序中使用地理围栏,除了删除触发的地理围栏外,一切正常。 我从 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));
    }
}

他被触发后如何删除地理围栏?

【问题讨论】:

    标签: android geofencing android-geofence


    【解决方案1】:

    你可以这样做:

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

    【讨论】:

      【解决方案2】:

      您将像添加地理围栏时一样继续操作(创建一个LocationClient 并等待它连接)。连接后,在onConnected 回调方法中,您将在LocationClient 实例上调用removeGeofences,并将您要删除的请求ID 列表和OnRemoveGeofencesResultListener 实例作为回调处理程序传递给它。

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

      @Override
      public void onConnected(Bundle arg0) {
          locationClient.removeGeofences(requestIDsList, 
          new OnRemoveGeofencesResultListener() {
          ...     
      });
      

      【讨论】:

      • 那么我在Service中创建一个LocationClient?
      • 不知道,你添加地理围栏的时候在哪里创建的?
      • LocationClient 不再包含在播放服务中。
      • 在同一个地理围栏退出时删除。您必须使用 LocationServices.GeofencingApi.removeGeofences(mGoogleApiClient, addGeofences().getGeofencePendingIntent()).setResultCallback(this);
      猜你喜欢
      • 1970-01-01
      • 2016-01-01
      • 2015-11-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多