Reputation: 1839
I am building several apps that all include a library module I created. In this library module I defined a generic purpose dialog that broadcasts the "buy" intent like this:
@Override public void onClick(View view) {
int i = view.getId();
if (i == R.id.btnYes) {
view.getContext().sendBroadcast(new Intent(PayComponent.ACTION_OPEN_PAY_SCREEN)
.putExtra(PayComponent.EXTRA_FROM_PURCHASE_DIALOG, true)
.putExtra(PayComponent.EXTRA_PURCHASE_PACKAGE_NAME, packageName)
);
} else if (i == R.id.btnNo) {
//
}
dismiss();
}
The problem here is that when I click on this dialog in the app A when I have both apps A and B opened (the broadcast receivers are not registered in manifest, but run-time in activities), both apps will receive the intent and act accordingly (open the purchase activity). The behaviour I want to achieve is that only the app A (the one that emitted the broadcast) will be able to receive it, so basically the intent is not broadcasted to the system.
PS: Both apps are signed using the same debug key, but they also use the same release key. I am pretty sure there is an easy solution to this issue, but I can't seem to find it in the docs.
PS2: I don't want to use explicit intents, as the library has no knowledge of the components that will receive the intent. It only knows that they should be within the same app.
Upvotes: 0
Views: 518
Reputation: 93561
Use LocalBroadcastManager to send broadcasts only to your current app. Register via LocalBroadcastManager as well (instead of BroadcastManager).
Upvotes: 2