I am aware of how BroadcastReceiver
feature/functionality works in Android and I've implemented multiple actions that my app will either receive or send.
However, whenever one of my applications receives a broadcast I want to know which application sent that broadcast.
Example (I have three applications):
Application A
and Application B
are allowed to send a broadcast to Application C
.
When Application C
receives the broadcast, I want to know which application sent this broadcast, either Application A
or Application B
?
Note: I am aware that I can add a extra
in the Intent
and send the package name however I am looking for a field that is "auto-generated". I do not want to do the following each time:
intent.putExtra("package_name", getPackageName());
Reason, you ask? Above is just an example. There could be more than two applications sending broadcasts and I don't want them to send package name in the Intent
each time.
I also noticed there is getPackage()
method for Intent
, however that always returns null
.
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
switch (action) {
case MY_GLOBAL_ACTION:
// this is where I want the package name of the application that sent the broadcast
intent.getPackage(); // this is always null
break;
default:
break;
}
}
I want to know which application sent this broadcast, either Application A or Application B?
Add it as an extra.
I am looking for a field that is "auto-generated".
There is none. By default, an Intent
does not contain information about the app that created the Intent
, any more than a HashMap
does.
I also noticed there is getPackage() method for Intent, however that always returns null.
Presumably, that is because you are not calling setPackage()
. And, since that package does not identify the sender, it will not help you.