简体   繁体   中英

How can I pass data from an Activity to a Geofence BroadcastReceiver?

I was building a Geofencing Application to track the number of people in the given region at any given time and storing their IDs on a Backend. I am unable to figure out how to pass the ID I take from the user and to the GeofenceBroadcastReceiver where I am incrementing and decrementing the number of people in the onReceive so I can keep a track of who all are in there. I don't think it can be passed through an intent as I am not the one calling the Receiver and I am unfamiliar of the inner workings of the process.

MainActivity.java

public void addGeofence(LatLng latLng, float radius) {

        Geofence geofence = geofenceHelper.getGeofence(GEOFENCE_ID, latLng, radius, Geofence.GEOFENCE_TRANSITION_ENTER | Geofence.GEOFENCE_TRANSITION_DWELL | Geofence.GEOFENCE_TRANSITION_EXIT);
        GeofencingRequest geofencingRequest = geofenceHelper.getGeofencingRequest(geofence);
        PendingIntent pendingIntent = geofenceHelper.getPendingIntent();

geofencingClient.addGeofences(geofencingRequest, pendingIntent).addOnSuccessListener(
\\code );

GeofenceHelper.java

public class GeofenceHelper extends ContextWrapper {

    PendingIntent pendingIntent;

    public GeofenceHelper(Context base) {
        super(base);
    }

    public GeofencingRequest getGeofencingRequest(Geofence geofence) {
        return new GeofencingRequest.Builder()
                .addGeofence(geofence)
                .setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER)
                .build();
    }

    public Geofence getGeofence(String ID, LatLng latLng, float radius, int transitionTypes) {
        return new Geofence.Builder()
                .setCircularRegion(latLng.latitude, latLng.longitude, radius)
                .setRequestId(ID)
                .setTransitionTypes(transitionTypes)
                .setLoiteringDelay(5000)
                .setExpirationDuration(Geofence.NEVER_EXPIRE)
                .build();
    }

    public PendingIntent getPendingIntent() {
        if (pendingIntent != null) {
            return pendingIntent;
        }
        Intent intent = new Intent(this, GeofenceBroadcastReceiver.class);
        pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

        return pendingIntent;
    }

No need to pass data from activity to GeofenceBroadcastReceiver . When user enters geofence area, GeofenceBroadcastReceiver onReceive() automatically will be called by android system if you properly registered geofence.

First register your geofence once added successfully, in your emulator set your registered geofence location and move around to trigger enter and exit of geofence. When triggered, onReceive() automatically will be called and you can get triggered id from onReceive() intent. Then you can increment and decrement number of people with in onReceive()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM