Android Geofence在进入/退出时触发,没有错误,但没有GEOFENCE_TRANSITION_标志

时间:2017-01-05 09:47:17

标签: android android-geofence google-location-services

我正在使用下面的代码监控地理围栏转换

yhist, xhist, _hist = plt.hist(clean['v'],bins=40, range=(0, Maxv), color='g')
plt.ylabel("Number")

ax=clean['v'].plot(kind='density') #I would like to insert here a normalization to max(yhist)/max(ax)
ax.set_xlim(0, Maxv)
plt.xlabel("Orbital velocity (km/s)")
ax.get_yaxis().set_visible(False)

这就是我构建GeofencingRequest的方式

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

Geofence在进入和退出时正确触发,但是当我使用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_

请告知我在这里缺少什么

1 个答案:

答案 0 :(得分:1)

根据问题中的信息,我看不到您的代码有任何具体问题,但我怀疑您正在处理的Intent是不是来自转换警报?

我个人使用广播接收器从地理围栏接收Intent,使用了IntentFilter 过滤它。我使用的结构如下: -

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 

使用Activity onCreate方法注册此接收器: -

    // 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'
    }
}

我希望这会有所帮助。