【问题标题】:I can not make the event work only once (BeaconManager.RangingListener())我不能让事件只工作一次(BeaconManager.RangingListener())
【发布时间】:2015-02-09 09:51:07
【问题描述】:

我在我的项目中使用Estimote/Android-SDK我有一个问题。 在我来到Proximity.IMMEDIATE 区域的 Beacon 时,我向服务器发送了一个请求,它轮换了我的结果。

beaconManager.setRangingListener(new BeaconManager.RangingListener() {
            @Override
            public void onBeaconsDiscovered(final Region region, final List<Beacon> beacons) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        getSupportActionBar().setSubtitle("Found beacons: " + beacons.size());
                        adapter.replaceWith(beacons);
                        for (Beacon beacon : beacons){
                            if (beacon.getMinor()==22222 && Utils.computeProximity(beacon)== Utils.Proximity.IMMEDIATE){
                                String android_id = Settings.Secure.getString(context.getContentResolver(),
                                        Settings.Secure.ANDROID_ID);
                                UserApiConnector.me().getQueue(android_id);
                            }
                        }
                    }
                });
            }
        });

它有效。 但问题是当我在Proximity.IMMEDIATE 区域时,此事件会连续触发。我还需要让这个事件只工作一次。

【问题讨论】:

    标签: android estimote


    【解决方案1】:

    回调onBeaconsDiscovered 每秒调用一次,不仅在第一次看到信标时。

    如果你下次不想触发代码,你需要使用像wasSomethingDone这样的布尔值守卫。它被初始化为false,并在你的代码执行后设置为true

    那就写if (wasSomethingDone &amp;&amp; beacon.getMinor()==22222 &amp;&amp; Utils.computeProximity(beacon)== Utils.Proximity.IMMEDIATE) ...吧。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-25
      • 1970-01-01
      • 1970-01-01
      • 2012-01-24
      • 2021-01-07
      • 1970-01-01
      • 2018-01-28
      相关资源
      最近更新 更多