简体   繁体   English

如何将数据从活动传递到地理围栏 BroadcastReceiver?

[英]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.我正在构建一个地理围栏应用程序来跟踪给定区域在任何给定时间的人数并将他们的 ID 存储在后端。 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.我无法弄清楚如何将我从用户那里获取的 ID 传递给GeofenceBroadcastReceiver ,我在其中增加和减少onReceive中的人数,以便我可以跟踪那里的所有人。 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 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地理围栏助手.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 .无需将数据从活动传递到GeofenceBroadcastReceiver When user enters geofence area, GeofenceBroadcastReceiver onReceive() automatically will be called by android system if you properly registered geofence.当用户进入地理围栏区域时,如果您正确注册了地理围栏,android 系统将自动调用GeofenceBroadcastReceiver onReceive()

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.触发时,会自动调用onReceive() ,您可以从onReceive()意图中获取触发的 id。 Then you can increment and decrement number of people with in onReceive()然后你可以在onReceive()中增加和减少人数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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