2017-01-05 23 views
1

我使用的代码监测地理围栏过渡下面Android的地理栅栏上没有错误,但

LocationServices.GeofencingApi 
    .addGeofences(AssistApplication.getApiHelper().getClient(), 
        getGeofencingRequest(), 
        getPendingIntent(context, 
           GeofenceTransitionIntentService.class)) 
    .setResultCallback(this); 

没有GEOFENCE_TRANSITION_标志进入/退出触发这是我如何建立GeofencingRequest

private GeofencingRequest getGeofencingRequest() 
{ 
    return new GeofencingRequest.Builder() 
      .setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER | 
        GeofencingRequest.INITIAL_TRIGGER_EXIT) 
      .addGeofence(getGeofence()) 
      .build(); 
} 

private Geofence getGeofence() 
{ 
    return new Geofence.Builder() 
      .setRequestId(requestId) 
      .setCircularRegion(latitude, longitude, 100) 
      .setExpirationDuration(Geofence.NEVER_EXPIRE) 
      .setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER | 
        Geofence.GEOFENCE_TRANSITION_EXIT) 
      .build(); 
} 

地理栅栏触发正确的进入和退出,但是当我使用getGeofenceTransition(),我没有得到任何这三个GEOFENCE_TRANSITION_标志。

protected void onHandleIntent(Intent intent) 
{ 
    final GeofencingEvent event = GeofencingEvent.fromIntent(intent); 
    if (event.hasError()) 
    { 
     Log.e(TAG, getErrorMessage(event.getErrorCode())); 
     return; 
    } 

    switch (event.getGeofenceTransition()) 
    { 
     case Geofence.GEOFENCE_TRANSITION_EXIT: 
      // Not triggered 
      break; 
     case Geofence.GEOFENCE_TRANSITION_ENTER: 
     case Geofence.GEOFENCE_TRANSITION_DWELL: 
      // Not triggered 
      break; 
     default: 
      // Triggered on exit and enter 
    } 
} 

请告知我是缺少在这里

+0

你在你的'GeofencingRequest.Builder'设置?你从getGeofenceTransition()返回了什么?如果它是-1,那么你的意图不是来自转换警报。您的地理围栏事件是否在广播接收器中收到? – SimonH

+0

我在构建请求的地方添加了代码。我设置'INITIAL_TRIGGER_ENTER'和'INITIAL_TRIGGER_EXIT'初始触发和'GEOFENCE_TRANSITION_ENTER'和'GEOFENCE_TRANSITION_EXIT'过渡。我没有得到任何'GEOFENCE_TRANSITION_'标志,所以根据API参考它必须是-1,但我知道它是没有错误触发的,因为当我输入/退出区域时执行'default'代码。如果它不是转换警报并且没有错误,那么触发该服务的是什么?我没有使用任何广播接收器只有'IntentService' –

回答

1

从这个问题,我看不到你的代码的任何具体问题的信息,但我怀疑你正在处理的Intent是不是从转换警报?

我个人使用广播接收器从Geofence接收IntentIntentFilter使用 来过滤它。我使用的结构如下: -

申报广播接收器在您的Activity

private BroadcastReceiver passedGeofenceReceiver = new BroadcastReceiver() { 
     @Override 
     public void onReceive(Context context, Intent intent) { 
      int geofenceTransition; 

      // get the Geofencing Event 
      GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent); 
      if (geofencingEvent.hasError()) { 
       return; 
      } 

      // Get the transition type. 
      geofenceTransition = geofencingEvent.getGeofenceTransition(); 

      if (geofenceTransition == Geofence.GEOFENCE_TRANSITION_ENTER) { 
       // etc etc 

注册这个接收器在您的ActivityonCreate方法: -

// register this Activity as the receiver of Geofence notifications 
    IntentFilter filter = new IntentFilter(); 
    filter.addAction("com.example.HIT_GEOFENCE"); 
    this.registerReceiver(this.passedGeofenceReceiver, filter); 

在创建广播PendingIntent的,组滤波器(mGeofencePendingIntent是成员PendingIntent变量): -

private PendingIntent getGeofencePendingIntent() { 
    // Reuse the PendingIntent if we already have it. 
    if (mGeofencePendingIntent != null) { 
     return mGeofencePendingIntent; 
    } 

    Intent hitGeofenceIntent = new Intent("com.example.HIT_GEOFENCE"); //- intent to send a broadcast 

    mGeofencePendingIntent = PendingIntent.getBroadcast(this, 0, hitGeofenceIntent, PendingIntent.FLAG_UPDATE_CURRENT); 
    return mGeofencePendingIntent; 
} 

监控您的地理围栏: -

private void monitorGeofences() { 
    if (<check permissions>) { 
     GeofencingRequest geofencingRequest = getGeofencingRequest(); 
     PendingIntent pendingIntent = getGeofencePendingIntent(); 
     LocationServices.GeofencingApi.addGeofences(mGoogleApiClient, geofencingRequest, pendingIntent) 
         .setResultCallback(this); // Result callback fires 'onResult' 
    } 
} 

我希望这有助于。

+0

活动不活跃的地理围栏转变发生的时间,我不希望它启动。我只想显示通知。这仍然有效吗? –

+0

您的应用程序结构如何?您可以将广播接收器作为独立课程,或者在服务中声明。我建议如果您只是在没有运行“活动”的情况下等待Geofence触发,那么您将其编写为广播接收器应用程序组件。对不起,我的回答没有用。 – SimonH

+0

我工作在不同的项目,现在,所以我还没有尝试过你的答案。会让你知道它是否有效。 –