I have a intent service that receives explicit intents from other activities on what to do, and currently returns an implicit intent with the data as extras, like below:
Intent someImplicitIntent= new Intent(APP_PACKAGE_NAME + ACTION_SOME_DATA);
someImplicitIntent.putExtra(APP_PACKAGE_NAME + EXTRA_SOME_DATA, "HELLO");
sendBroadcast(someImplicitIntent);
Software design wise, I have multiple activities within the app, that when running, would use the identical data for doing different things. So, currently I have setup broadcast receivers in each activity to listen for "APP_PACKAGE_NAME + EXTRA_SOME_DATA" and then act accordingly, like interrupting something. The broadcast receivers are tied to the their respective activities' life-cycles
This setup is fine for testing functionality. But the problem is, I understand that implicit intents in Android broadcasts the intents system wide, so any app which a registered broadcast receiver listening to the action would received the intent and its extra. Implicit intents are great for its intended purposes, but for me the convenience of minimal code and sending just one and multiple recipients are carrying obvious security risks.
I'm thinking of making explicit intents, but my current & limited understanding is I would have to create an explicit intent for each activity in order to keep the data private within the scope of the application, like below:
Intent someExplicitIntent= new Intent(this, activityOne.class);
someExplicitIntent.setAction(APP_PACKAGE_NAME + ACTION_SOME_DATA)
someExplicitIntent.putExtra(APP_PACKAGE_NAME + EXTRA_SOME_DATA, "Hi");
sendBroadcast(someExplicitIntent);
Intent someExplicitIntent2= new Intent(this, activityTwo.class);
someExplicitIntent2.setAction(APP_PACKAGE_NAME + ACTION_SOME_DATA)
someExplicitIntent2.putExtra(APP_PACKAGE_NAME + EXTRA_SOME_DATA, "Hi");
sendBroadcast(someExplicitIntent2); ..... so on, for the same data to N activities
The snippet above look needlessly redundant, and will be wasting resources sending intents that won't be picked up. How can I securely send one intent extra to multiple receivers within an application?
LocalBroadcastManager. - Mike M.LocalBroadcastManager, 'cause it doesn't work with any broadcasts or Receivers on aContext. That's usually the mistake first users make, but it sounds like you've got it. Cheers! - Mike M.