3

我已经运行了一个 Geofence 示例,并分配了两组 Lat 、 Lng 和 radius 并运行该示例。当我点击注册地理围栏时,我的日志中出现了以下内容

Location Services is available
Client connected
Add Geofences: Success GeofenceRequestIds=[1, 2]
com.example.android.geofence.ACTION_GEOFENCES_ADDED

但在那之后什么也没有发生,我不知道通过启用 gps 将我传递到当前位置的问题是什么,但没有效果。

我不知道何时会在越过围栏时收到通知,为了测试它,我已经通过了围栏中的相同地理坐标,但我仍然没有收到任何警报。

在通过 stackoverflow 阅读了几个线程之后,我什至尝试用内部函数替换getService()getBroadcast()但没有运气。createRequestPendingIntent

我也试过了

**Geofence #1 and Geofence #2**

Lat : 23.039568000000003
Lng : 72.56600400000002
Radius :1

而且我还将我当前在 genymotion 中的位置设置为高于坐标以使其工作但没有通知。

即使在真实设备上,我的 3G 也已启用,并且我正在使用该设备移动,但到目前为止没有任何变化。

4

2 回答 2

2

当您增加半径时,您设置的半径太小 google-play 服务不会发出 1 米的通知,它会为此提供更好的结果,并尝试将其用于地理围栏。

在地理围栏接收器中

Intent broadcastIntent = new Intent();
@Override
public void onReceive(Context context, Intent intent) {
    this.context = context;

    broadcastIntent.addCategory(GeofenceUtils.CATEGORY_LOCATION_SERVICES);

    if (LocationClient.hasError(intent)) {
        handleError(intent);
    } else {
        handleEnterExit(intent);
    }
}

private void handleError(Intent intent) {
    // Get the error code
    int errorCode = LocationClient.getErrorCode(intent);

    // Get the error message
    String errorMessage = LocationServiceErrorMessages.getErrorString(
            context, errorCode);

    // Log the error
    Log.e(GeofenceUtils.APPTAG, context.getString(
            R.string.geofence_transition_error_detail, errorMessage));

    // Set the action and error message for the broadcast intent
    broadcastIntent.setAction(GeofenceUtils.ACTION_GEOFENCE_ERROR)
            .putExtra(GeofenceUtils.EXTRA_GEOFENCE_STATUS, errorMessage);

    // Broadcast the error *locally* to other components in this app
    LocalBroadcastManager.getInstance(context).sendBroadcast(
            broadcastIntent);
}

private void handleEnterExit(Intent intent) {
    // Get the type of transition (entry or exit)
    int transition = LocationClient.getGeofenceTransition(intent);

    // Test that a valid transition was reported
    if ((transition == Geofence.GEOFENCE_TRANSITION_ENTER)
            || (transition == Geofence.GEOFENCE_TRANSITION_EXIT)) {

        // Post a notification
        List<Geofence> geofences = LocationClient
                .getTriggeringGeofences(intent);
        String[] geofenceIds = new String[geofences.size()];
        String ids = TextUtils.join(GeofenceUtils.GEOFENCE_ID_DELIMITER,
                geofenceIds);
        String transitionType = getTransitionString(transition);
        sendNotification(transitionType, ids);

        for (int index = 0; index < geofences.size(); index++) {
            Geofence geofence = geofences.get(index);

        }
        // Create an Intent to broadcast to the app
        broadcastIntent
                .setAction(GeofenceUtils.ACTION_GEOFENCE_TRANSITION)
                .addCategory(GeofenceUtils.CATEGORY_LOCATION_SERVICES);

        LocalBroadcastManager.getInstance(sqlitewraper.context)
                .sendBroadcast(broadcastIntent);

        // Log the transition type and a message
        Log.d(GeofenceUtils.APPTAG, transitionType + ": " + ids);
        Log.d(GeofenceUtils.APPTAG, context
                .getString(R.string.geofence_transition_notification_text));

        // In debug mode, log the result
        Log.d(GeofenceUtils.APPTAG, "transition");

        // An invalid transition was reported
    } else {
        // Always log as an error
        Log.e(GeofenceUtils.APPTAG, context.getString(
                R.string.geofence_transition_invalid_type, transition));
    }
}

/**
 * Posts a notification in the notification bar when a transition is
 * detected. If the user clicks the notification, control goes to the main
 * Activity.
 * 
 * @param transitionType
 *            The type of transition that occurred.
 * 
 */

private void sendNotification(String transitionType, String locationName) {

    // Create an explicit content Intent that starts the main Activity
    Intent notificationIntent = new Intent(sqlitewraper.context,
            MainActivity.class);

    // Construct a task stack
    TaskStackBuilder stackBuilder = TaskStackBuilder
            .create(sqlitewraper.context);

    // Adds the main Activity to the task stack as the parent
    stackBuilder.addParentStack(MainActivity.class);

    // Push the content Intent onto the stack
    stackBuilder.addNextIntent(notificationIntent);

    // Get a PendingIntent containing the entire back stack
    PendingIntent notificationPendingIntent = stackBuilder
            .getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

    // Get a notification builder that's compatible with platform versions
    // >= 4
    NotificationCompat.Builder builder = new NotificationCompat.Builder(
            sqlitewraper.context);

    // Set the notification contents
    builder.setSmallIcon(R.drawable.ic_notification)
            .setContentTitle(transitionType + ": " + locationName)
            .setContentText(
                    sqlitewraper.context
                            .getString(R.string.geofence_transition_notification_text))
            .setContentIntent(notificationPendingIntent);

    // Get an instance of the Notification manager
    NotificationManager mNotificationManager = (NotificationManager) sqlitewraper.context
            .getSystemService(Context.NOTIFICATION_SERVICE);

    // Issue the notification
    mNotificationManager.notify(0, builder.build());
}

 private String getTransitionString(int transitionType) {
        switch (transitionType) {

            case Geofence.GEOFENCE_TRANSITION_ENTER:
                return ("enter geofence");

            case Geofence.GEOFENCE_TRANSITION_EXIT:
                return ("exit from  geofence");

            default:

        return ("Transisation unknown");
        }
 }

示例代码 GeofenceRequester 的更改

    Intent intent = new Intent("packagename.ACTION_RECEIVE_GEOFENCE");

//  Intent intent = new Intent(mActivity, ReceiveTransitionsIntentService.class);


return PendingIntent.getBroadcast(

                   sqlitewraper.context,
                    0,
                   intent,
                    PendingIntent.FLAG_UPDATE_CURRENT);
        }

清单文件中的更改

<receiver android:name="packagename.GeofenceReceiver"
            android:exported="false" >
            <intent-filter>
                <action android:name="packagename.ACTION_RECEIVE_GEOFENCE" />
              <category android:name="packagename" />
            </intent-filter>
        </receiver>
于 2014-07-04T09:26:08.703 回答
0

嘿,检查一下是否选择了访问我的位置。即在设置-> 位置访问

于 2014-07-04T10:28:11.247 回答