Reputation: 1269
I wondering if it is possible to create a private broadcast. I actually register broadcastreceiver in my activity and use
sendOrderedBroadcast(broadcast);
method to send the broadcast. But, for now, the intent (broadcast) used for this method is defined like this :
Intent broadcast = new Intent("com.mypackage.broadcast");
So every external application which declare this package name can listen what I am sending, and I don't want to.
So how to make this impossible, that no one can listen my broadcast ?
Upvotes: 8
Views: 3160
Reputation: 213
For LocalBroadcast Manager to work the app should be running.
To have a generic strategy to limit Android broadcasts to your own app only, we can do as follow.
Intent intent = new Intent();
String packageName = context.getApplicationContext().getPackageName();
intent.setAction(packageName + "<MY_ACTION>");
context.sendBroadcast(intent);
Since the application package would remain unique for all apps on android, this would safely limit the Broadcast to your own app and you can register BroadcastReceivers in AndroidManifest.xml.
Upvotes: 1
Reputation: 26007
I think you are looking for LocalBroadcast Manager. The docs say:
It is a helper to register for and send broadcasts of Intents to local objects within your process. This is has a number of advantages over sending global broadcasts with sendBroadcast(Intent). One of them is that the data you are broadcasting won't leave your app, so don't need to worry about leaking private data.`
See how to use LocalBroadcastManager? for more. Hope it helps you.
Upvotes: 6