1

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" />
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
Waqas
  • 6,812
  • 2
  • 33
  • 50

1 Answers1

2

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.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • following is what i have in createIntentToBroadcastOnCompletion() { return new Intent(locationPollerParameter.getIntentToBroadcastOnCompletion()); } where getIntentToBroadcastOnCompletion() return exactly the same string which is "com.commonsware.cwac.locpoll.EXTRA_INTENT" – Waqas Apr 10 '12 at 14:44
  • @Waqas: Then `"com.commonsware.cwac.locpoll.EXTRA_INTENT"` is not the same as `com.commonsware.cwac.locpoll.LocationPollerParameter.INTENT_TO_BROADCAST_ON_COMPLETION_KEY`. – CommonsWare Apr 10 '12 at 14:51
  • thanks for your quick replies- com.commonsware.cwac.locpoll.LocationPollerParameter.INTENT_TO_BROADCAST_ON_COM‌​PLETION_KEY is a static string which has value com.commonsware.cwac.locpoll.EXTRA_INTENT" – Waqas Apr 10 '12 at 14:54
  • @Waqas: Here is a sample project using ordered broadcasts: https://github.com/commonsguy/cw-advandroid/tree/master/Broadcast/Ordered -- you will need to determine where what you are doing for your broadcasts differs from what is in this sample. – CommonsWare Apr 10 '12 at 15:07
  • thanks! you were right regarding IntentFilter- the action value was not the same – Waqas Apr 10 '12 at 15:49