如何解决Android 中地理围栏半径中的多个呼叫问题
我的 ionic 应用程序 android 版本中遇到有关地理围栏的问题。半径出/入条目被多次调用。可以在radius中进入或从radius中出去时,应该正确调用一次。
我们也在使用 eventbus。
呼叫地理围栏
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 举报,一经查实,本站将立刻删除。