Search code examples
javaandroidbroadcastreceiver

How to securely send one intent extra to multiple receivers within an Android application


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?


Solution

  • You can make use of LocalBroadcastManager to send broadcasts which can be recieved only by the components of your application, thereby making it secure.

    So, you can do as follows :

        Intent someImplicitIntent= new Intent(APP_PACKAGE_NAME + ACTION_SOME_DATA);
        someImplicitIntent.putExtra(APP_PACKAGE_NAME + EXTRA_SOME_DATA, "HELLO");
        LocalBroadcastManager.getInstance(this).sendBroadcast(someImplicitIntent);
    

    Thereafter, just register your broadcast receiver in those components in which you want the intent extra/s.

    In this way, you will be able to send intent extra/s through local broadcasts.

    For more information about LocalBroadcastManager, you can check out the following link :

    https://developer.android.com/reference/android/support/v4/content/LocalBroadcastManager.html