【问题标题】:Android geofencing doesn't working with dynamically geofences loaded (volley request)Android 地理围栏不适用于动态加载的地理围栏(齐射请求)
【发布时间】:2015-04-17 10:59:53
【问题描述】:

我正在尝试从 api 动态加载地理围栏,但我遇到了问题。 当我将静态数据加载到我的应用程序中(例如硬编码)时,一切顺利(地理围栏快速触发)但是如果我尝试从远程服务器加载数据,地理围栏操作将不再触发。我使用 Volley 来处理请求。

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

获取数据的 Volley 请求

public void geofencePointsRequest(final Context context, Map<String,String> params, final ProgressDialog pDialog){
    webService = new WebService();

    CustomArrayRequest jsonObjReq = new CustomArrayRequest(
            Request.Method.GET,
            WebServiceUrl.POINTS,
            params,
            new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {
                    ArrayList<Point> points = webService.getPoints(response);
                    populateGeofenceList(points);
                }
            },

            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    String errorHandler = webService.getError(error);
                    int status = (error.networkResponse.statusCode != 0) ? error.networkResponse.statusCode : 0;
                    Toast.makeText(context, "Code : " + status + " - Error : " + errorHandler, Toast.LENGTH_LONG).show();
                }
            }) {

        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            return webService.getHeaders();
        }
    };

    VolleySingleton.getInstance(context).addToRequestQueue("pointsRequest", jsonObjReq);
}

构建 GoogleApiClient

protected synchronized void buildGoogleApiClient() {
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API)
            .build();
}

@Override
protected void onStart() {
    super.onStart();
    mGoogleApiClient.connect();
}

在连接时我会加载地理围栏

@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());
    }
}

编辑:我在代码中重构了 buildGoogleApiClient 位置,但问题始终是它不会触发通知...

感谢您的帮助

【问题讨论】:

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


    【解决方案1】:

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

    所以您的问题可能是 Google Api Client 为空,因此未连接。在这里检查:

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

    【讨论】:

    • 不幸的是我删除了这个但不是他的问题:(使用常量工作......没有动态数据:-(
    • 地理围栏 ID 有什么问题?
    • 哦,我明白了,所以我猜最后一个覆盖了前一个。
    • 不,问题是我有很多地理围栏,其中一些具有相同的 id...(pe id 为 1,2,3,1,4,3...,这会导致问题)。
    猜你喜欢
    • 1970-01-01
    • 2015-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多