如何将Geofence过渡值从广播接收器传输到我的主活动



我正在我的应用程序中的一个功能,当用户退出地理围栏时,将标记移动到某个位置。

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


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

}

我尝试过使用intent,但是因为我是初学者而失败了。

使用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 );
} 

最新更新