我在我的Android应用中使用地理围栏link。
我正在我的默认可见活动中创建并添加geofencing对象,并使用上面链接中所述的IntentService帮助监听其事件。
Geofence对象已成功添加,但当我使用真实设备对其进行测试并移入/移出已定义的地理围栏区域时,其ENTER / EXIT事件不会被触发。
我已经测试了它的半径为100米,50米,500米,1500米和2000米,但在任何情况下它都无法正常工作。
这就是我创建和添加地理围栏的方式:
mGeofencingClient = LocationServices.getGeofencingClient(DashboardActivity.this);
Geofence geofence = new Geofence.Builder()
// Set the request ID of the geofence. This is a string to identify this
// geofence.
.setRequestId(REQUEST_ID)
.setCircularRegion(
22.703617,
75.873409,
Constants.GEOFENCE_RADIUS_IN_METERS
)
.setExpirationDuration(Geofence.NEVER_EXPIRE)
.setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER |
Geofence.GEOFENCE_TRANSITION_EXIT )
.build();
mGeofenceList.add(geofence);
GeofencingRequest geofencingRequest = new GeofencingRequest.Builder()
.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER|GeofencingRequest.INITIAL_TRIGGER_EXIT)
.addGeofences(mGeofenceList).build();
Intent intents = new Intent(this, GeofenceTransitionsIntentService.class);
mGeofencePendingIntent = PendingIntent.getService(DashboardActivity.this, 0, intents, PendingIntent.
FLAG_UPDATE_CURRENT);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}else {
mGeofencingClient.addGeofences(geofencingRequest, mGeofencePendingIntent)
.addOnSuccessListener(this, new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Log.e("geofencing success", "successfully added");
}
}).addOnFailureListener(this, new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.e("geofencing failure", "failed in added");
}
});
}
&#13;
要收听我正在使用的IntentService的ENTER / EXIT事件,如下所示:
public class GeofenceTransitionsIntentService extends IntentService {
public GeofenceTransitionsIntentService(String name) {
super(name);
}
protected void onHandleIntent(Intent intent) {
GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent);
Toast.makeText(this,"service get start",Toast.LENGTH_LONG).show();
Log.e("service","geofencing service started");
if (geofencingEvent.hasError()) {
String errorMessage = GeofenceErrorMessages.getErrorString(this,
geofencingEvent.getErrorCode());
Toast.makeText(this,"error_in_geofancing"+errorMessage,Toast.LENGTH_LONG).show();
return;
}
// Get the transition type.
int geofenceTransition = geofencingEvent.getGeofenceTransition();
// Test that the reported transition was of interest.
if (geofenceTransition == Geofence.GEOFENCE_TRANSITION_ENTER ||
geofenceTransition == Geofence.GEOFENCE_TRANSITION_EXIT || geofenceTransition == Geofence.GEOFENCE_TRANSITION_DWELL) {
// Get the geofences that were triggered. A single event can trigger
// multiple geofences.
List<Geofence> triggeringGeofences = geofencingEvent.getTriggeringGeofences();
Toast.makeText(this,"you are inside range",Toast.LENGTH_LONG).show();
// Get the transition details as a String.
String geofenceTransitionDetails = getGeofenceTransitionDetails(geofenceTransition, triggeringGeofences);
// Send notification and log the transition details.
sendNotification(geofenceTransitionDetails);
Log.e("geofancing","you are inside range");
} else {
Toast.makeText(this,"Invalid_Transition",Toast.LENGTH_LONG).show();
}
}
private String getGeofenceTransitionDetails(
// Context context,
int geofenceTransition,
List<Geofence> triggeringGeofences) {
String geofenceTransitionString = getTransitionString(geofenceTransition);
// Get the Ids of each geofence that was triggered.
ArrayList <String>triggeringGeofencesIdsList = new ArrayList<>();
for (Geofence geofence : triggeringGeofences) {
triggeringGeofencesIdsList.add(geofence.getRequestId());
}
String triggeringGeofencesIdsString = TextUtils.join(", ", triggeringGeofencesIdsList);
return geofenceTransitionString + ": " + triggeringGeofencesIdsString;
}
}
&#13;
我为此添加了ACCESS_FINE_LOCATION和LOCATION_HARDWARE的权限。