Search code examples
javaandroidbroadcastreceivercommonsware-cwac

LocationPoller and multiple broadcast receiver


I am using cwac-Location Poller (from here) to constantly poll user location and display location based notifications. This all is working fine but now I am trying to attach another BroadcastReceiver so that if my application is in foreground, instead of displaying notification animate the google map to current user location. But for some reason I can't get it working.

onCreate() method of MapActivity I have following code to start poller:

@Override
public void onCreate(Bundle savedInstanceState) {
   .....

    alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

    Intent i = new Intent(this, LocationPoller.class);

    Bundle bundle = new Bundle();
    LocationPollerParameter parameter = new LocationPollerParameter(bundle);
    parameter.setIntentToBroadcastOnCompletion(new Intent(this, LocationReceiver.class));
    parameter.setProviders(new String[] {LocationManager.GPS_PROVIDER, LocationManager.NETWORK_PROVIDER});
    parameter.setTimeout(60000);
    i.putExtras(bundle);

    pendingIntent = PendingIntent.getBroadcast(this, 0, i, 0);

    alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, 
                SystemClock.elapsedRealtime(), PERIOD, pendingIntent);                                 
}

in onResume() method I am registering another receiver using registerReceiver() method:

@Override
protected void onResume() {
    super.onResume();
    IntentFilter intentFilter = new IntentFilter(com.commonsware.cwac.locpoll.LocationPollerParameter.INTENT_TO_BROADCAST_ON_COMPLETION_KEY);
    intentFilter.setPriority(1);
    registerReceiver(locationReceiver, intentFilter);
}

Where locationReceiver looks like:

private BroadcastReceiver locationReceiver = new BroadcastReceiver() {      
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d(TAG, "mapActivity called");
        abortBroadcast();
    }
};

And in order to send ordered broadcast to multiple receiver I have modified LocationPollerService to use sendOrderedBroadcast instead of sendBroadcast

public void onLocationChanged(Location location) {
      handler.removeCallbacks(onTimeout);
      Intent toBroadcast = createIntentToBroadcastOnCompletion();

      toBroadcast.putExtra(LocationPollerResult.LOCATION_KEY, location);        
      sendOrderedBroadcast(toBroadcast, null);
      quit();
}

Now the problem is my dynamically registered receiver never get called but the one mentioned in AndroidManifest.xml does:

    <receiver android:name=".receiver.LocationReceiver" />
    <receiver android:name="com.commonsware.cwac.locpoll.LocationPoller" />
    <service android:name="com.commonsware.cwac.locpoll.LocationPollerService" />

Solution

  • Your problem is in a disconnect between the IntentFilter you create in Java and your createIntentToBroadcastOnCompletion() implementation, which you did not include in your question. Your IntentFilter is expecting a broadcast with a certain action string -- the Intent you are creating in createIntentToBroadcastOnCompletion() apparently does not include this action string.

    BTW, with respect to "And in order to send broadcast to multiple receiver", sendBroadcast() is perfectly capable of sending broadcasts to multiple receivers.