当前位置: 首页 > 知识库问答 >
问题:

Android Geofence addGeofences

黄宏旷
2023-03-14

我对Android系统中的地理围栏有问题。我按照https://developer.android.com/training/location/geofencing.html的说明建立了地理位置的列表,我连接到LocationClient,它工作了,它返回连接,我可以得到最后一个位置...但当我执行:

mLocationClient.AddGeOfences(infoapp.mGeOfenceList,mTransitionPendingIntent,this);

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

我使用以下代码创建地理围栏:

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调用这个函数:

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时,没有收到更多的响应。

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

}

非常感谢!

共有1个答案

林博厚
2023-03-14

您是否已等待调用AddGeOfences()直到连接了LocationClient

 类似资料:

相关问答

相关文章

相关阅读