我正在遵循地理围栏指南,但我无法获得LocalBroadcastManager
类别有效的意图。Intent 发送者发送 2 种具有相同类别的 Intent GeofenceUtils.CATEGORY_LOCATION_SERVICES
:
- 有行动GeofenceUtils.ACTION_GEOFENCES_ADDED
Intent broadcastIntent = new Intent();
String msg;
broadcastIntent.setAction(GeofenceUtils.ACTION_GEOFENCES_ADDED)
.addCategory(GeofenceUtils.CATEGORY_LOCATION_SERVICES)
.putExtra(GeofenceUtils.EXTRA_GEOFENCE_STATUS, msg);
LocalBroadcastManager.getInstance(mActivity).sendBroadcast(broadcastIntent);
- 有行动GeofenceUtils.ACTION_GEOFENCE_ERROR
Intent broadcastIntent = new Intent();
String msg;
broadcastIntent.setAction(GeofenceUtils.ACTION_GEOFENCE_ERROR)
.addCategory(GeofenceUtils.CATEGORY_LOCATION_SERVICES)
.putExtra(GeofenceUtils.EXTRA_GEOFENCE_STATUS, msg);
LocalBroadcastManager.getInstance(mActivity).sendBroadcast(broadcastIntent);
在接收器中,我想要一个IntentFilter
按类别而不是按动作过滤意图,如下所示:
geofenceReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// Get extra data included in the Intent
String message = intent.getStringExtra(GeofenceUtils.EXTRA_GEOFENCE_STATUS);
Log.d(TAG, "geofenceReceiver got message: " + message);
}
IntentFilter intentFilter = new IntentFilter();
intentFilter.addCategory(GeofenceUtils.CATEGORY_LOCATION_SERVICES);
LocalBroadcastManager.getInstance(this).registerReceiver(geofenceReceiver, intentFilter);
但onReceive
永远不会触发。为什么?