Android 中地理围栏半径中的多个呼叫问题

如何解决Android 中地理围栏半径中的多个呼叫问题

我的 ionic 应用程序 android 版本中遇到有关地理围栏的问题。半径出/入条目被多次调用。可以在radius中进入或从radius中出去时,应该正确调用一次。

我们也在使用 eventbus。

这里我们在android文件中有代码

呼叫地理围栏

int geofenceTransition = geofencingEvent.getGeofenceTransition();
    // Test that the reported transition was of interest.
    if (geofenceTransition == Geofence.GEOFENCE_TRANSITION_ENTER ||
            geofenceTransition == Geofence.GEOFENCE_TRANSITION_EXIT) {

        Intent i = new Intent("geofenceStatus");
        if (geofenceTransition == Geofence.GEOFENCE_TRANSITION_EXIT) {
            status = "exit";
        } else {
            if (status.equals("exit")) status = "reenter";
            else status = "enter";
        }

        Date currentTime = Calendar.getInstance().getTime();
        i.putExtra("status",status);
        LocalbroadcastManager.getInstance(this).sendbroadcast(i);
        EventBus.getDefault().post(status);

        // Get the geofences that were triggered. A single event can trigger multiple geofences.
        List<Geofence> triggeringGeofences = geofencingEvent.getTriggeringGeofences();

        // Get the transition details as a String.
        String geofenceTransitionDetails = getTransitionString(geofenceTransition);//getGeofenceTransitionDetails(geofenceTransition,triggeringGeofences);

        // Send notification and log the transition details.
        sendNotification(geofenceTransitionDetails);
    } else {
        // Log the error.
    }

添加地理围栏

mGeofencingClient.addGeofences(getGeofencingRequest(),getGeofencePendingIntent())
        .addOnFailureListener(e -> {
          Log.e(TAG,"onFailure: " + e.toString());
          //Toast.makeText(activity,"Failed to add geoFence," + e.toString(),Toast.LENGTH_SHORT).show();
        })
        .addOnCompleteListener(task -> {
          Log.d(TAG,"onComplete: GeoFence added status: " + task.isSuccessful());
          if (task.isSuccessful()) {
            Calendar c = Calendar.getInstance();
            geofenceCreateDate = String.valueOf(c.get(Calendar.DATE));
            Toast.makeText(activity,"GeoFence Added",Toast.LENGTH_SHORT).show();
            PrefUtils.savetoPrefs(activity,"geofenceCreateDate",geofenceCreateDate);
          }
        });

删除地理围栏

mGeofencingClient.removeGeofences(getGeofencePendingIntent()).addOnCompleteListener(task -> {
    Log.d(TAG,"removeGeofences: GeoFence remove status: " + task.isSuccessful());
    PrefUtils.removeFromPrefs(activity,"GEO_STATUS");
    //if (task.isSuccessful())
    // Toast.makeText(activity,"GeoFence Removed",Toast.LENGTH_SHORT).show();
  });

客户端调用请求如下

client.newCall(request).enqueue(new Callback() {
  @Override
  public void onFailure(Call call,IOException e) {
    Log.e(TAG,"onFailure: api Failed" + e.getLocalizedMessage());
    call.cancel();
  }

  @Override
  public void onResponse(Call call,Response response) {
    try {
      //response.body() can be consumed only once so use only jsonData
      String jsonData = response.body().string();
      Log.d(TAG,"onResponse: " + jsonData);

      JSONObject jsonObj = new JSONObject(jsonData);
      String status = jsonObj.getString("status");
      if (status != null && status.equals("success")) {
        JSONArray jsonRes = jsonObj.getJSONArray("result");
        JSONObject obj = jsonRes.getJSONObject(0);

        latitude = Double.parseDouble(obj.getString("latitude"));
        longitude = Double.parseDouble(obj.getString("longitude"));
        //GEOFENCE_RADIUS_IN_METERS = Integer.parseInt(obj.getString("geofencing_metre"));
        removeGeofence();
        addGeoFence();
      }
    } catch (Exception e) {
      e.printstacktrace();
    }
  }
});

这是我们打印的一些日志,显示了多次调用

2021-04-23 09:45:58 - {"id":0,"user_id":"127","job_id":"945","task_code_id":0,"action":"out","time_entry_id":0,"latitude":"42.3430848","longitude":"-85.2745107","app_version":20200}
2021-04-23 09:45:58 - {"id":0,"job_id":"847","latitude":"42.2929321","longitude":"-85.5819675","job_id":"775","latitude":"42.3501563","longitude":"-86.0732105","app_version":20200}
2021-04-23 09:45:58 - {"id":0}
2021-04-23 09:46:01 - {"id":0,"action":"in","app_version":20200}
2021-04-23 09:46:01 - {"id":0}
2021-04-23 09:46:01 - {"id":0}
2021-04-23 09:46:01 - {"id":0}
2021-04-23 09:46:01 - {"id":0,"app_version":20200}
2021-04-23 09:46:01 - {"id":0}
2021-04-23 09:46:01 - {"id":0}
2021-04-23 09:46:01 - {"id":0,"app_version":20200}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?