2

I'm creating an app using the new Android build system, and using a LocalBroadcastManager to fire notifications from an IntentService back to an Activity.

I've defined the following product flavours as part of the build for 3 separate apps;

productFlavours {
    london {
        packageName 'com.example.lo'
    }
    dubai {
        packageName 'com.example.du'
    }
}

When I build the app using either of these product flavours I am not getting any broadcasts through the manager. I've confirmed that both the service and the activity are running in the same process.

A broadcast receiver is bound as required in the Activity#onCreate method of my activity but does not receive any events. I have also confirmed by printing out the process ID of both the activity and service and both are reporting that this is the same process, so this can't be an issue.

As a test I removed the product flavours and built the app with just the single packagename as defined in the app manifest file and now the broadcast manager seems to function as expected. On the intents that I'm sending I am only setting the action.

Do I need to set anything else?

marcus.ramsden
  • 2,633
  • 1
  • 22
  • 33
  • are you sure you are registering the receiver with correct intent-filters? perhaps show some code where it takes place – waqaslam Sep 26 '13 at 11:43
  • Yep checked and double checked the intent filter. I'm registering it on the onCreate method for the activity and unregistering in onDestroy. – marcus.ramsden Sep 26 '13 at 12:46
  • are you using the same context for firing and receiving broadcast? because LocalBroadcasts are bound to the Context. – waqaslam Sep 26 '13 at 13:40
  • Yeah I dumped out the process id in the activity and service to make sure that the message was staying within the same one. As I mentioned the broadcasts work fine when I comment out the product flavours and build the app as a single flavour. – marcus.ramsden Sep 26 '13 at 14:00
  • @Waqas Must be same context for firing and receiving local broadcast? Are you sure? Could have sworn I've broadcast on activity context and received on service or app context (and vica-versa), and I think this is a common use case. – Tom Sep 27 '13 at 23:43
  • 1
    @Tom Yeah it doesn't have to be exactly the same context object. It can come from a service and an activity for example. Obviously the context has to come from the same app, but I'm pretty sure it's impossible to get a context from another app. The key restriction for local broadcast is that it can't cross a process boundary. – marcus.ramsden Sep 28 '13 at 08:08
  • Strange problem indeed. Not an answer to your question, but I would recommend using EventBus. Much simpler API, and probably better performance. – Ertan D. Sep 28 '13 at 08:50
  • @ErtanD Thanks for the suggestion, as a solution I fell on the Otto side of things as I've used it before. I do need to look more into EventBus though as I note that they claim better performance than Otto, although it's not been a huge issue for me just yet, premature optimisation and all that. – marcus.ramsden Sep 28 '13 at 09:01
  • @Tom & marcus.ramsden: context could be of two types, Application context and Activity context. They both serve the same purpose but differentiate from each other at some point. I was referring to application context which is same throughout the application's life-cycle, and yes, you cannot hold other application's context. If you could, then its a major security breach. For more info read [this](http://tinyurl.com/olsgtek), [this](http://tinyurl.com/nwr8rt6) and [this](http://tinyurl.com/blzakjq) – waqaslam Sep 28 '13 at 09:35

2 Answers2

1

If you look at the source code of LocalBroadcastManager you'll see that if you give your Intent the flag Intent.FLAG_DEBUG_LOG_RESOLUTION (by using Intent.addFlags(int)), it will print in the logcat pretty useful information: you might want to try that with and without the flavours and compare the result to see what goes wrong.

Takhion
  • 2,706
  • 24
  • 30
0

I have org.cmpny.app and org.cmpny.app.additional namespaces in same project; LocalBroadcastManager works fine, nothing is required to be tuned in AndroidManifest (except that LocalBroadcastManager can't be used with ":remote" services, but that's another point)

kagali-san
  • 2,964
  • 7
  • 48
  • 87