Я пытаюсь создать геозону, а затем что-то делать (например, показывать уведомления, беззвучный мобильный), когда пользователь входит или выходит из местоположения.
Следуя этому руководству https://developer.android.com/training/location/geofencing.html#HandleGeofenceTransitions
Несмотря на то, что геозона создается, ничего не происходит, когда устройство входит в местоположение (элемент управления не переходит в класс GeofenceTrasition.class) и даже если оно выполняет geoFenceTransition == Geofence.GEOFENCE_TRANSITION_ENTER не выполняется
Я проверил предыдущие вопросы и руководства, но ничто не помогает. Я также прошел github (не понимаю этот код должным образом)
Код MapsActivity для геозоны
private void buildGeofence () {
Log.d (TAG, "buildGeofence: BuilderExecuted");
mGeofenceList.add(new Geofence.Builder()
.setRequestId("A1")
.setCircularRegion(address.getLongitude(), address.getLatitude(), GEOFENCE_RADIUS_IN_METERS)
.setExpirationDuration(Geofence.NEVER_EXPIRE)
.setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER | Geofence.GEOFENCE_TRANSITION_EXIT)
.build());
System.out.println("Longitude is " + address.getLongitude() + "Latitude is" + address.getLatitude());
mGeofencingClient.addGeofences(getGeofencingRequest(), getGeofencePendingIntent())
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Log.d(TAG, "onSuccess: GeoFencing Successful");
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.d(TAG, "onFailure: GeoFencing UnSuccessful");
}
});
}
Circle geofenceLimits;
public void addGeofenceCircle(){
CircleOptions circleOptions = new CircleOptions()
.center(m1.getPosition())
.strokeColor(Color.argb(50,70,70,70))
.fillColor(Color.argb(100,150,150,150))
.radius(GEOFENCE_RADIUS_IN_METERS);
geofenceLimits = mMap.addCircle(circleOptions);
}
private GeofencingRequest getGeofencingRequest() {
Log.d(TAG, "getGeofencingRequest: Geofencing request executed");
GeofencingRequest.Builder builder = new GeofencingRequest.Builder();
builder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER);
builder.addGeofences(mGeofenceList);
return builder.build();
}
private PendingIntent getGeofencePendingIntent() {
if (mGeofencePendingIntent != null) {
return mGeofencePendingIntent;
}
Log.d(TAG, "getGeofencePendingIntent: Geofencing intenet executed");
Intent Intent = new Intent(this, GeofenceTransition.class);
mGeofencePendingIntent = PendingIntent.getService(MapsActivity.this, 0, Intent, PendingIntent.
FLAG_UPDATE_CURRENT);
return mGeofencePendingIntent;
}
Класс для обработки намерения и определения, находится ли устройство внутри или вне геозоны.
открытый класс GeofenceTransition расширяет IntentService {
private static final String TAG = "GeofenceIntent";
public static final int GEOFENCE_NOTIFICATION_ID = 0;
public GeofenceTransition() {
super("GeofenceTransition");
}
@Override
protected void onHandleIntent(Intent intent) {
Log.d(TAG, "onHandleIntent: Executed");
GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent);
if (geofencingEvent.hasError()) {
String error = String.valueOf(geofencingEvent.getErrorCode());
Toast.makeText(getApplicationContext(), "Error = " + error, Toast.LENGTH_SHORT).show();
Log.e(TAG, "onHandleIntent: Intent Error");
return;
}
int geoFenceTransition = geofencingEvent.getGeofenceTransition();
int i=geoFenceTransition;
System.out.println(i);
System.out.println("Checking Geofence Transition");
if (geoFenceTransition == Geofence.GEOFENCE_TRANSITION_ENTER ||
geoFenceTransition == Geofence.GEOFENCE_TRANSITION_EXIT) {
Log.d(TAG, "onHandleWork: Enter/Exit Works");
Toast.makeText(getApplicationContext(), "WORKING", Toast.LENGTH_SHORT).show();
}
}