【发布时间】:2017-08-19 18:03:45
【问题描述】:
我正在构建一个移动应用程序,我可以使用它来测试地理围栏在 Android 手机上的工作情况。我有一个将GoogleApiClient 用于FusedLocationApi.requestLocationUpdates 的应用程序。我正在获取我在 MapFragment 中的地图上显示的所有更新。然后我尝试使用GeofencingClient 添加地理围栏。
private void addGeofenceList() {
mGeofencingClient.addGeofences(getGeofencingRequest(), getGeofencePendingIntent())
.addOnSuccessListener(this, new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Log.wtf(TAG, "addGeofences() - succesfully added");
}
})
.addOnFailureListener(this, new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.wtf(TAG, "addGeofences() - failed to add");
}
});
}
此代码已执行,我返回它们已成功添加。但是当我在地理围栏应该触发它的位置时,不要调用 IntentService。我还添加了 IntentService:
private PendingIntent getGeofencePendingIntent() {
if (mGeofencePendingIntent != null) {
return mGeofencePendingIntent;
}
Intent intent = new Intent(this, GeofenceService.class);
return PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
我还创建了一个GeofencingRequest,我在其中设置了输入时的初始触发器。
private GeofencingRequest getGeofencingRequest() {
GeofencingRequest.Builder builder = new GeofencingRequest.Builder()
.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER)
.addGeofences(mGeofenceList);
return builder.build();
}
这是我创建 ArrayList<Geofence> mGeofenceList 的地方,我在其中添加了所有地理围栏:
public void createGeofenceList() {
GeofenceList geofences = new GeofenceList();
ArrayList<GeofenceModel> geofenceList = (ArrayList) geofences.returnGeofences();
for (GeofenceModel geofence : geofenceList) {
mGeofenceList.add(new Geofence.Builder()
.setRequestId(geofence.getREQ_ID())
.setCircularRegion(
geofence.getLocation().latitude,
geofence.getLocation().longitude,
geofence.getRadius()
)
.setExpirationDuration(geofence.getExpire())
.setTransitionTypes(geofence.getTransition())
.build());
}
addGeofenceList();
}
我的整个项目都在GitHub。我们将不胜感激所有帮助!
【问题讨论】:
标签: java android google-api-client geofencing android-geofence