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

无法在地理Geofence中得到警报

干宏邈
2023-03-14
Location Services is available
Client connected
Add Geofences: Success GeofenceRequestIds=[1, 2]
com.example.android.geofence.ACTION_GEOFENCES_ADDED

在通过stackoverflow读取几个线程之后,我甚至尝试在CreateRequestPendingIntent函数中用GetBroadcast()替换GetService(),但没有成功。

我也试过

**Geofence #1 and Geofence #2**

Lat : 23.039568000000003
Lng : 72.56600400000002
Radius :1

而且我也设置了我目前在genymotion的位置以上的坐标,使其工作,但没有通知。

共有1个答案

宦琪
2023-03-14

你设置的半径太小,谷歌播放服务不给1米的通知,当你增加半径,它给了更好的结果,为地理围栏尝试。

在GeofenceReceiver中

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>
 类似资料:
  • 如何从多个地理位置(长,晚值)创建多边形地理围栏。也可以在Android上跟踪用户进入或退出该地理区域的情况。

  • 根据API文件,只允许圆形土工篱笆: 但我有4个位置,代表矩形的4个角,我想让我的地理围栏是那个矩形。 我想避免建立我的自定义位置监控服务的解决方案扩展的功能监测地理围栏,因为我认为这类服务是CPU和功耗消耗。 谢谢,

  • 有没有人知道如何在Android中创建一个多边形的地理围栏?我知道Android支持圆形(即lat/long+radius)地理围栏,但我需要对它们的边界进行更精确的控制。

  • 每次我运行< code>pip时,都会出现以下警告。 警告:旧脚本包装程序正在调用pip。这将在未来版本的pip中失败。请看https://github.com/pypa/pip/issues/5599寻求解决潜在问题的建议。为了避免这个问题,您可以使用“-m pip”调用Python,而不是直接运行pip。 请帮我理解一下。Pip警告 编辑1。我尝试重新安装pip,但警告仍然存在。此外,我还附上

  • 问题内容: 在过去的两天里,我在docker上遇到了麻烦,我可以解决。在docker doc之后,您可以公开容器将侦听与的连接的端口。到目前为止,一切都很好! 如果我的应用程序侦听端口8080,则应使用公开我的docker容器,并使用将其绑定到主主机的端口80 。 这是我的Dockerfile: 而我只是汝南&&。 我有一个简单的express nodejs应用程序: 这是我构建docker映像的

  • 我需要一些关于Geofence guide提供的示例代码的明确性,如下所示: https://developer.android.com/training/location/geofencing.html 我运行了这段代码,我看到地理围栏是正确创建的,但我真正想要的是一种方法,当我开车到这些地理围栏位置时,能够得到警报。现在,当我经过这些地理隔离点时,什么也没有发生(ReceiveTransiti