Changing Activity when User entered the geofence

21 views Asked by At

How to change activity when a User entered the Geofence?

public class GeofenceBroadcastReceiver extends BroadcastReceiver {

    private static final String TAG = "GeofenceBroadcastReceive";

    @Override
    public void onReceive(Context context, Intent intent) {

        // Toast.makeText(context, "Geofence triggered...", Toast.LENGTH_SHORT).show();

        GeofencingEvent geofencingEvent = intent != null ? GeofencingEvent.fromIntent(intent) : null;

        if (geofencingEvent != null && geofencingEvent.hasError()) {
            String errorMessage = GeofenceStatusCodes.getStatusCodeString(geofencingEvent.getErrorCode());
            Log.e(TAG, "onReceive: " + errorMessage);
            return;
        }
        List<Geofence> geofenceList = geofencingEvent != null ? geofencingEvent.getTriggeringGeofences() : null;
        if (geofenceList != null) {
            for (Geofence geofence: geofenceList) {
                Log.d(TAG, "onReceive: " + geofence.getRequestId());
            }
        }
        int transitionType = geofencingEvent != null ? geofencingEvent.getGeofenceTransition() : 0;

        switch (transitionType) {
            case Geofence.GEOFENCE_TRANSITION_ENTER:
                Toast.makeText(context, "GEOFENCE_TRANSITION_ENTER", Toast.LENGTH_SHORT).show();
                intent = new Intent(this, MainActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK);
                startActivity(intent);
                break;
            case Geofence.GEOFENCE_TRANSITION_DWELL:
                Toast.makeText(context, "GEOFENCE_TRANSITION_DWELL", Toast.LENGTH_SHORT).show();
                break;
            case Geofence.GEOFENCE_TRANSITION_EXIT:
                Toast.makeText(context, "GEOFENCE_TRANSITION_EXIT", Toast.LENGTH_SHORT).show();
                break;
        }

I have tried to insert intent = new Intent in the switch case, but it won't work of course, what was I expecting.

0

There are 0 answers