Atal Sohail
Atal Sohail

Reputation: 1

Geofence isn't triggering when device enters a location

I am trying to create a geofence and then do some stuff (like show notification, silent mobile) when user enter or exit a location.

Following this guide https://developer.android.com/training/location/geofencing.html#HandleGeofenceTransitions

Although the geofence is being created, nothing happens when the device enters the location (Control isn't going to the GeofenceTrasition.class) and even if it does the geoFenceTransition == Geofence.GEOFENCE_TRANSITION_ENTER doesn't execute

I have checked previous questions and guides but nothing is helping I have also gone through the github (don't understand that code properly)

MapsActivity Code for Geofence

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;
}

Class to handle the intent and tell if the device is inside or outside the geofence public class GeofenceTransition extends 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();


    }
}

Upvotes: 0

Views: 170

Answers (1)

Atal Sohail
Atal Sohail

Reputation: 1

I am dumb so I set geofence longitude and latitude like : .setCircularRegion(address.getLongitude(), address.getLatitude(), GEOFENCE_RADIUS_IN_METERS)

While the first value is for latitude and second one is for longitude, correct implemntation would be: .setCircularRegion(address.getLatitude(), address.getLongitude(), GEOFENCE_RADIUS_IN_METERS)

Upvotes: 0

Related Questions