当用户“驻留”时,删除BroadcastReceiver类中的地理围栏

问题描述

因此,我基本上在我的应用程序中添加了一些地理栅栏,现在我想在用户停留在半径范围内时将其删除。我的GeofencebroadcastReceiver已这样实现:

@Override
    public void onReceive(Context context,Intent intent) {
        
        GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent);

        TextToSpeech mTTS = new TextToSpeech(context,status -> {
            if (status == TextToSpeech.SUCCESS) {
                int result = mTTS.setLanguage(Locale.ENGLISH);
                if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                    Log.e("TTS","Language not supported");
                }
            } else {
                Log.e("TTS","Initialisation Failed");
            }
        });


        if (geofencingEvent.hasError()) {
            Log.d(TAG,"onReceive: Error receiving geofence event");
            return;
        }

        List<Geofence> geofenceList = geofencingEvent.getTriggeringGeofences();
        for (Geofence geofence : geofenceList) {
            Log.d(TAG,"onReceive: " + geofence.getRequestId());
        }
        int transitionType = geofencingEvent.getGeofenceTransition();

        switch (transitionType) {
            case Geofence.GEOFENCE_TRANSITION_ENTER:
                Toast.makeText(context,"Elevation alert",Toast.LENGTH_SHORT).show();
                break;
            case Geofence.GEOFENCE_TRANSITION_DWELL :
                Toast.makeText(context,"GEOFENCE_TRANSITION_DWELL",Toast.LENGTH_SHORT).show();

                break;
        }
}

我认为我可以使用意图删除已触发的地理围栏(我不知道触发器是指进入,停留还是退出),而这篇文章几乎解决了我的问题: remove triggered geofence after transition

但是,我只想删除当前驻留的地理围栏,如果要使用上述方法,则需要访问位于我的主要活动中的地理围栏客户端,以调用remove geofences方法

我还想添加更好的输出方式(即不只是举杯消息),例如振动等(您可以看到textToSpeech对象)。我收到一条错误消息,提示它尚未初始化。

跟进:

为了澄清另一件事,我相信调用map.clear()会删除当前地图上的所有地理围栏吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)