对于所有三个问题,答案都是肯定的。
第一个,你可以在Geofence的Builder中确定你想要得到的精度,像这样
new Geofence.Builder()
.setRequestId(key)
.setCircularRegion(lat, lang, 150)
.setExpirationDuration(Geofence.NEVER_EXPIRE)
.setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER)
.setLoiteringDelay(1000)
.build();
我将精度设置为 150m(您应该知道的一件事是,您设置的精度越高,使用的功率就越多)
对于第二个和第三个,您可以将TransitionTypes 设置为Geofence.GEOFENCE_TRANSITION_DWELL 以了解用户是否在一个地方停留一段时间。同时,您可以使用 PendingIntent 在此条件匹配时发送广播。完整代码如下
Geofence geofence = getGeofence(lat, lng, key);
geofencingClient.addGeofences(
getGeofencingRequest(geofence),
getGeofencePendingIntent(title, location, id))
.addOnCompleteListener(task -> {
if (task.isSuccessful()) {
}else{
}
});
getGeofencingRequest的代码
private GeofencingRequest getGeofencingRequest(Geofence geofence) {
GeofencingRequest.Builder builder = new GeofencingRequest.Builder();
builder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER);
builder.addGeofence(geofence);
return builder.build();
}
getGeofencePendingIntent的代码
private PendingIntent getGeofencePendingIntent(String title, String location, long id) {
Intent i = new Intent("add your unique ID for the broadcast");
Bundle bundle = new Bundle();
bundle.putLong(Reminder.ID, id);
bundle.putString(Reminder.LOCATION_NAME, location);
bundle.putString(Reminder.TITLE, title);
i.putExtras(bundle);
return PendingIntent.getBroadcast(
getContext(),
(int) id,
i,
0
);
}