Search code examples
android-studiobroadcastreceiverandroid-geofence

BroadcastReceiver triggered to update another Activity


I am trying to make a feature as part of my android app, where a user interacts with a geofence based on their location on a map and it will fire up a dialog telling the user they are near the starting point of the route using a BroadcastReceiver in its own class.

So far I can trigger it and provide Toast messages, but I can't seem to use it to trigger a UI change in my other activity.

Here is my BroadcastReceiver class -

public class GeofenceBroadcastReceiver extends BroadcastReceiver {

    private static final Object TAG = "Error";


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

        GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent);

        if (geofencingEvent.hasError()) {
            Log.d("TOASTY", "onReceive: Geofence even has error..");
        }

        List<Geofence> triggeredGeofenceList = geofencingEvent.getTriggeringGeofences();

        for (Geofence geofence : triggeredGeofenceList) {

            Log.d("GEOF", "onReceive: "+geofence.getRequestId());

        }

        Location triggerLocation = geofencingEvent.getTriggeringLocation();

        double lat = triggerLocation.getLatitude();
        double lon = triggerLocation.getLongitude();


        Toast.makeText(context, "GEOFENCE TRIGGERED AT : LAT IS :" + lat + " LON IS : " +lon, Toast.LENGTH_SHORT).show();

        int transitionType = geofencingEvent.getGeofenceTransition();

        switch (transitionType) {

            case Geofence.GEOFENCE_TRANSITION_ENTER:
                Toast.makeText(context, "Entered Geofence", Toast.LENGTH_SHORT).show();
                Log.d("GEOF", "onReceive: "+geofencingEvent.getGeofenceTransition());
                break;
            case Geofence.GEOFENCE_TRANSITION_DWELL:
                Toast.makeText(context, "Dwelling inside of Geofence", Toast.LENGTH_SHORT).show();
                break;
            case Geofence.GEOFENCE_TRANSITION_EXIT:
                Toast.makeText(context, "Exited Geofence area", Toast.LENGTH_SHORT).show();
                break;



        }

        Bundle b = intent.getExtras();

        Intent i = new Intent(context, routeActivity.class);
        i.putExtra("lat", lat);
        i.putExtra("lon", lon);
        i.putExtras(b);

        Log.d("LOLCALLY", "onReceive: "+i);

        context.sendBroadcast(i);


    }

}

My thinking was to use intent, I have tried to pull the triggered location (which I can see is correct in the log output) into my other activity but no joy.

Many thanks!


Solution

  • You need to register your receiver on your activity and process its callback:

    public class MyActivity extends AppCompatActivity {
        private BroadcastReceiver geofenceReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                // Pull triggered location and use it to update the activity
            }
        };
    
        @Override
        protected void onResume() {
            super.onResume();
            registerReceiver(geofenceReceiver, new IntentFilter("YOUR_GEOFENCE_ACTION"));
        }
    
        @Override
        protected void onPause() {
            super.onPause();
            unregisterReceiver(geofenceReceiver);
        }
    }