首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >安卓Geofence addGeofences

安卓Geofence addGeofences
EN

Stack Overflow用户
提问于 2014-03-10 20:55:07
回答 2查看 1.1K关注 0票数 0

我在Android中遇到了地理围栏的问题。我按照https://developer.android.com/training/location/geofencing.html的指示建立了geofence列表,我与LocationClient连接,它工作,它返回连接,我可以获得最后一个位置……但是当我执行的时候:

mLocationClient.addGeofences(InfoApp.mGeofenceList, mTransitionPendingIntent, this);

我在onAddGeofencesResult中没有响应。我在logcat中看不到任何消息。

我用下面的代码创建了geofence:

代码语言:javascript
复制
mUIGeofence1 = new SimpleGeofence(
infoEstablecimiento.getString("_id"),
infoEstablecimiento.getDouble("latitud"),
infoEstablecimiento.getDouble("longitud"),
(float) infoEstablecimiento.getDouble("radio"),
Geofence.NEVER_EXPIRE,
// This geofence records only entry transitions with a delay
Geofence.GEOFENCE_TRANSITION_DWELL, loiteringDelay);

首先,我用REQUEST_TYPE.ADD调用这个函数:

代码语言:javascript
复制
public static void conexionGeofence(
        REQUEST_TYPE mRequestType,
        Context contexto,
        SlidingFragmentActivity actividad,
        GooglePlayServicesClient.ConnectionCallbacks connectionAct,
        GooglePlayServicesClient.OnConnectionFailedListener connectionListener) {
    // Start a request to add geofences
    /*
     * Test for Google Play services after setting the request type. If
     * Google Play services isn't present, the proper request can be
     * restarted.
     */
    if (!servicesConnected(actividad)) {
        return;
    }
    /*
     * Create a new location client object. Since the current activity class
     * implements ConnectionCallbacks and OnConnectionFailedListener, pass
     * the current activity object as the listener for both parameters
     */
    InfoApp.mLocationClient = new LocationClient(contexto, connectionAct,
            connectionListener);

    // If a request is not already underway
    if (!InfoApp.mInProgress) {
        InfoApp.mRequestType = mRequestType;

        // Indicate that a request is underway
        InfoApp.mInProgress = true;
        // Request a connection from the client to Location Services
        InfoApp.mLocationClient.connect();
    } else {
        /*
         * A request is already underway. You can handle this situation by
         * disconnecting the client, re-setting the flag, and then re-trying
         * the request.
         */
    }
}

我在onConnected中收到了响应,但是当我调用addGeofences时,没有收到更多的响应。

代码语言:javascript
复制
@Override
public void onConnected(Bundle connectionHint) {
    /*
     * Choose what to do based on the request type set in removeGeofences
     */
    switch (InfoApp.mRequestType) {
    case ADD:
        // Get the PendingIntent for the request
        InfoApp.mTransitionPendingIntent = GeofenceUtils
                .getTransitionPendingIntent();

        Location loc = InfoApp.mLocationClient.getLastLocation();

        // Send a request to add the current geofences
        InfoApp.mLocationClient.addGeofences(InfoApp.mGeofenceList,
                InfoApp.mTransitionPendingIntent, this);
        break;

    case REMOVE_LIST:
        InfoApp.mLocationClient.removeGeofences(InfoApp.mGeofencesToRemove,
                this);
        break;

    default:
        break;
    }

}

非常感谢!

EN

回答 2

Stack Overflow用户

发布于 2014-03-27 03:07:28

您是否一直等到LocationClient连接好后才调用addGeofences()

票数 0
EN

Stack Overflow用户

发布于 2014-07-01 18:50:16

考虑以下添加案例:

代码语言:javascript
复制
    // Get the PendingIntent for the request
    InfoApp.mTransitionPendingIntent = GeofenceUtils
            .getTransitionPendingIntent();

    Location loc = InfoApp.mLocationClient.getLastLocation();

    // Send a request to add the current geofences
    InfoApp.mLocationClient.addGeofences(InfoApp.mGeofenceList,
            InfoApp.mTransitionPendingIntent, this);
    break;

是否等待在连接LocationClient之前调用addGeofences()

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22300710

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档