简体   繁体   中英

How to transfer Geofence transition values from broadcast receiver to my main activity

I'm working on a feature in my app that moves a marker to a certain latlng when the user exits the geofence.

Send the broadcast received data to any activity


public class GeofenceBroadcastReceiver extends BroadcastReceiver {

    private static final String TAG = "GeofenceBroadcastReceiver";

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO: This method is called when the BroadcastReceiver is receiving
        // an Intent broadcast.
//        Toast.makeText(context, "Geofence triggered...", Toast.LENGTH_SHORT).show();

        NotificationHelper notificationHelper = new NotificationHelper(context);

        GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent);

        if (geofencingEvent.hasError()) {
            Log.d(TAG, "onReceive: Error receiving geofence event...");
            return;
        }

        List<Geofence> geofenceList = geofencingEvent.getTriggeringGeofences();
        for (Geofence geofence: geofenceList) {
            Log.d(TAG, "onReceive: " + geofence.getRequestId());
        }
//        Location location = geofencingEvent.getTriggeringLocation();
        int transitionType = geofencingEvent.getGeofenceTransition();


        switch (transitionType) {
            case Geofence.GEOFENCE_TRANSITION_ENTER:
                Toast.makeText(context, "You are near the drop off location.", Toast.LENGTH_SHORT).show();
                notificationHelper.sendHighPriorityNotification("You are near the drop off location", "", Home.class);
                break;
            case Geofence.GEOFENCE_TRANSITION_DWELL:
                Toast.makeText(context, "Pickup/Drop off Location reached.", Toast.LENGTH_SHORT).show();
                notificationHelper.sendHighPriorityNotification("Pickup/Drop off Location reached.", "", Home.class);
                break;
            case Geofence.GEOFENCE_TRANSITION_EXIT:
                Toast.makeText(context, "Leaving Pickup/Drop off point.", Toast.LENGTH_SHORT).show();
                notificationHelper.sendHighPriorityNotification("Leaving Pickup/Drop off point.", "", Home.class);
                break;
        }

    }


}

I tried using intents but failed due to me being a beginner.

Using LocalBroadcastManager, we may transfer data from onReceive to another activity.

@Override
    public void onReceive(Context context, Intent intent) { 
                Bundle extras = intent.getExtras();
                Intent intent = new Intent("broadCastName");  //new Intent(context, ReceiveText.class);
                // Data you need to pass to another activity
                intent .putExtra("message", extras.getString(Config.MESSAGE_KEY)); 
                context.sendBroadcast(intent );
    } 

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