触发后移除地理围栏

Gp2*_*mv3 5 android geofencing android-geofence

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

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

Jes*_*run 1

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

当然,您必须使用与创建 with 时使用的相同GeoFence请求GeoFence.BuilderID setRequestId

@Override
public void onConnected(Bundle arg0) {
    locationClient.removeGeofences(requestIDsList, 
    new OnRemoveGeofencesResultListener() {
    ...     
});
Run Code Online (Sandbox Code Playgroud)

  • LocationClient 不再包含在播放服务中。 (2认同)