Android GeoFence无法使用动态加载的GeoFence(截击请求)

Android GeoFence无法使用动态加载的GeoFence(截击请求),android,google-play-services,android-geofence,Android,Google Play Services,Android Geofence,我试图从api动态加载geofences,但我遇到了一个问题。 例如,当我将静态数据加载到硬编码的应用程序中时,geofence会快速触发,但如果我尝试从远程服务器加载数据,geofence操作将不再触发。我用截击来处理请求 onCreate方法 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layou

我试图从api动态加载geofences,但我遇到了一个问题。 例如,当我将静态数据加载到硬编码的应用程序中时,geofence会快速触发,但如果我尝试从远程服务器加载数据,geofence操作将不再触发。我用截击来处理请求

onCreate方法

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_activity);

    // Empty list for storing geofences.
    mGeofenceList = new ArrayList<Geofence>();

    // Initially set the PendingIntent used in addGeofences() and removeGeofences() to null.
    mGeofencePendingIntent = null;

    geofencePointsRequest(this, null, null);

    buildGoogleApiClient();
}
在连接的I-load GeoFence上

@Override
public void onConnected(Bundle connectionHint) {
    Log.i(TAG, "Connected to GoogleApiClient");
    addGeofencesOnLoad();
}

private GeofencingRequest getGeofencingRequest() {
    GeofencingRequest.Builder builder = new GeofencingRequest.Builder();
    builder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER);
    builder.addGeofences(mGeofenceList);
    return builder.build();
}

public void addGeofencesOnLoad() {
    if (!mGoogleApiClient.isConnected()) {
        Toast.makeText(this, getString(R.string.not_connected), Toast.LENGTH_SHORT).show();
        return;
    }

    try {
        LocationServices.GeofencingApi.addGeofences(
                mGoogleApiClient,
                getGeofencingRequest(),
                getGeofencePendingIntent()
        ).setResultCallback(this); // Result processed in onResult().
    } catch (SecurityException securityException) {
        logSecurityException(securityException);
    }
}
我根据解析的数据创建一个地理围栏列表

public void populateGeofenceList(ArrayList<Point> points) {
    for (int i=0; i<points.size(); i++) {
        mGeofenceList.add(new Geofence.Builder()
                .setRequestId(pins.get(i).getShortId())
                .setCircularRegion(
                        points.get(i).getLocation().getCoordinates().latitude,
                        points.get(i).getLocation().getCoordinates().longitude,
                        points.get(i).getLocation().getRadius()
                )
                .setExpirationDuration(Geofence.NEVER_EXPIRE)
                .setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER)
                .build());
    }
}
编辑:我已经在代码中重构了BuildGoogleAppClient位置,但问题总是它不会触发通知


感谢您的帮助

您似乎没有初始化Google Api客户端,BuildGoogleAppClient方法在调用populateGeofenceList之前不会执行,如果您没有连接到Google Api客户端,则不会执行此操作

所以,您的问题可能是Google Api客户端为空,因此未连接。请点击此处:

 protected void onStart() {
    super.onStart();
    if (mGoogleApiClient != null) mGoogleApiClient.connect();
}

不幸的是,我删除了这个问题,但不是他的问题:常数可以工作…动态数据否:-geofence ID有什么问题吗?哦,我明白了,所以我想最后一个是覆盖了前一个。不,问题是我有很多geofence,有些具有相同的ID…p.e。ID为1,2,3,1,4,3…,这会引起问题。
public void populateGeofenceList(ArrayList<Point> points) {
    for (int i=0; i<points.size(); i++) {
        mGeofenceList.add(new Geofence.Builder()
                .setRequestId(pins.get(i).getShortId())
                .setCircularRegion(
                        points.get(i).getLocation().getCoordinates().latitude,
                        points.get(i).getLocation().getCoordinates().longitude,
                        points.get(i).getLocation().getRadius()
                )
                .setExpirationDuration(Geofence.NEVER_EXPIRE)
                .setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER)
                .build());
    }
}
 protected void onStart() {
    super.onStart();
    if (mGoogleApiClient != null) mGoogleApiClient.connect();
}