无法在地理围栏中收到警报

2024-03-20

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

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

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

我不知道当跨越栅栏时我什么时候会收到通知,为了测试它,我已经通过了栅栏中相同的地理坐标,但仍然没有收到警报。

我什至尝试过更换getService() by getBroadcast() inside createRequestPendingIntent在 stackoverflow 上阅读了一些线程后,函数,但没有运气。

我也尝试过

**Geofence #1 and Geofence #2**

Lat : 23.039568000000003
Lng : 72.56600400000002
Radius :1

我还将我在 genymotion 中的当前位置设置为上述坐标以使其工作,但没有通知。

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


您设置的半径太小,当您增加半径时,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>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

无法在地理围栏中收到警报 的相关文章

  • 删除 json 对象字符串中的“\”

    如何删除下面字符串中的特殊字符 String x message content toom recipients id 1000001865 room subject room 我使用了 x replaceAll 但它不起作用 您必须转义正
  • NumberPicker 的格式化值在单击时消失

    我的 NumberPicker 在setDescendantFocusability FOCUS BLOCK DESCENDANTS 模式和setWrapSelectorWheel false 已关闭 我用一个简单的格式化程序格式化了我的
  • Mesibo 通话 UI 未更新

    我正在尝试更改 Mesibo Call UI 的配置 但它并没有改变 我尝试如下 MesiboCallConfig mesiboCallConfig new MesiboCallConfig mesiboCallConfig backgro
  • Android中如何将文件写入raw文件夹?

    我认为这是一个非常基本的问题 我目前正在编写这样的文件 File output new File exampleout mid 现在 我想将文件写入 myproject res raw 我读到我可以通过将完整的网址放在 中来做到这一点 但
  • Android相当于javascript的setTimeout和clearTimeout?

    setTimeout 有一个答案https stackoverflow com a 18381353 433570 https stackoverflow com a 18381353 433570 它没有提供我们是否可以像在 JavaSc
  • 有没有办法替代Android中的标准Log?

    有没有办法以某种方式拦截对 android 中标准 Log 的调用并执行其他操作 在桌面 Java 中 人们通常会得到一些记录器 因此有多种方法可以安装不同的日志处理程序 实现 但是 Android似乎对Log有静态调用 我找不到任何有关替
  • 谷歌地图颤动检查点是否在多边形内

    我正在使用 google maps flutter 插件开发 flutter 项目 我想检查用户位置是否位于我在地图上创建的多边形内 有一个简单的方法使用 JavaScript api con tainsLocation 方法 但对于 fl
  • AOSP 中 android.Build.SERIAL 何时何地生成?

    我知道android Build SERIAL是在第一次设备启动时生成的 但我无法准确定位位置和时间 我正在建造AOSP Jelly Bean Android平板电脑 nosdcard 第二个问题 这个是序列号吗 really对所有人来说都
  • 当编辑文本获得焦点时更改边框颜色

    我想知道当编辑文本聚焦时如何更改它的边框颜色 目前它看起来像这样 我尝试过在SDK中检查源图片 但我无法理解它 我也尝试过使用xml 但无法仅更改边框颜色 如果我找到源图片 我可以在 Photoshop 中编辑以更改颜色 有什么关于如何执行
  • opencv人脸检测示例

    当我在设备上运行应用程序时 应用程序崩溃并显示以下按摩 java lang UnsatisfiedLinkError 无法加载 detector based tracker findLibrary 返回 null 我正在使用 OpenCV
  • 将 firebase auth 与 google app engine 云端点集成

    有人可以指定 使用一些示例代码 如何验证谷歌云端点中的 firebase 令牌吗 最近提出的问题根本没有澄清 如何将 Firebase 身份验证与 Google 应用引擎端点集成 https stackoverflow com questi
  • Flutter Spotify Api 身份验证

    我需要在使用 Spotify api 的 Flutter 应用程序中对用户进行身份验证 我使用 flutter web auth 打开 WebView 并让用户在那里登录 我无法返回应用程序 在 Spotify 仪表板中 我将回调 Uri
  • 取消通知

    我使用Onesignal推送通知 需要取消所有onPause和onResume的通知 NotificationManager notificationManager NotificationManager getApplicationCon
  • Android 26 (O) 通知不显示操作图标 [重复]

    这个问题在这里已经有答案了 随着 Android 26 O 引入通知渠道 我一直在调查 Google 提供的com example android notificationchannels 这个示例按预期工作 直到我尝试添加Action到示
  • 以编程方式应用样式资源

    我没有找到一种以编程方式做到这一点的方法 所以我在这里发布这个问题 我也没有找到与此相关的任何问题 我有一个资源样式 在 res values styles xml 中定义 我想做的是使用 java 将这种样式应用到我正在操作的 View
  • 活动组代码示例

    有人可以给我一些使用活动组的示例代码吗 我的应用程序中有一些按钮 我想将活动应用于这些按钮 目前我正在使用 setVisibility 但我被告知活动组将是更好的选择 这是另一个ActivityGroup 示例项目 http richipa
  • 如何将额外的文本添加到颤振谷歌地图自定义标记中?

    问题是如何将自定义谷歌地图标记上的文本重叠与代表车辆登记号的文本融合在一起 我尝试使用此方法将文本叠加在图标上 生成器 上下文 gt 但根本不被认可 class MapsDemo extends StatefulWidget overrid
  • 如何让用户在android列表视图中选择主题?

    我有一个带有两个标签的列表视图 标题和副标题 我想要深色和浅色背景作为用户选项 标题具有 textAppearanceMedium 副标题具有 textAppearanceSmall 我希望样式 MyTheme Dark 具有白色文本 My
  • 画布:尝试使用回收的位图错误

    我是一个相当新的程序员 所以任何建议将不胜感激 我有一个类 每次调用它时都会在循环中运行 AsyncTask AsyncTask 看起来像这样 public class LoadImageTask extends AsyncTask
  • 在DialogFragment中,onCreate应该做什么?

    我目前正在摆弄 DialogFragment 以学习使用它 我假设相比onCreateView onCreate 可以这样做 public void onCreate Bundle savedInstanceState super onCr

随机推荐