繁体   English   中英

如何将地理围栏转换值从广播接收器传输到我的主要活动

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

我正在开发我的应用程序中的一项功能,该功能可在用户退出地理围栏时将标记移动到某个 latlng。

将广播接收到的数据发送到任何活动


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;
        }

    }


}

我尝试使用意图但由于我是初学者而失败了。

使用 LocalBroadcastManager,我们可以将数据从 onReceive 传输到另一个活动。

@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 );
    } 

暂无
暂无

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

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