I'm following geofencing guidelines and I can't get LocalBroadcastManager
for intents with categories working. Intent sender sends 2 kinds of Intents with the same category GeofenceUtils.CATEGORY_LOCATION_SERVICES
:
-with action 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);
-with action 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);
In receiver I'd like to have an IntentFilter
which filters Intents by category not by action like so:
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);
But onReceive
is never triggered. Why?
OK I found the solution here http://developer.android.com/reference/android/content/IntentFilter.html:
So I have to add both
Action
s to theIntentFilter
: