If this flag is set in a broadcast Intent then it will only be delivered to those eligible BroadcastReceivers which were dynamically registered.
10.2 FLAG_RECEIVER_REPLACE_PENDING If this flag is set in a broadcast Intent then it will replace any broadcast Intent which matches it,
as defined by Intent.filterEquals()
, which is currently in the process of being delivered to any eligible BroadcastReceivers. This effect is not atomic. Some BroadcastReceivers may receive both the original and the replacement broadcast Intent,
others only the replacement, as the following rather contrived example demonstrates. We define two static BroadcastReceivers
each in a separate Application, and one dynamic BroadcastReceiver registered by a third Application as follows
IntentFilter f = new IntentFilter(); f.addAction("xper.receiver.intent.RECEIVER_LUNDY_INTENT"); f.addAction("xper.receiver.intent.RECEIVER_SEA_AREA_INTENT"); registerReceiver(new Lundy(), f We define their respective onReceive()
methods to be // Sole public void onReceive(Context context, Intent intent)
{
System.out.println("Sole.onReceive(..., " + intent + ")");
System.out.println("Sole.onReceive(...) N == " + intent.getStringExtra("N"));
}
// Fastnet public void onReceive(Context context, Intent intent)
{
System.out.println("Fastnet.onReceive(..., " + intent + ")");
System.out.println("Fastnet.onReceive(...) N == " + intent.getStringExtra("N"));
}
// Lundy public void onReceive(Context context, Intent intent)
{
System.out.println("Lundy.onReceive(..., " + intent + ")");
System.out.println("Lundy.onReceive(...) N == " + intent.getStringExtra("N"));
} If we execute the following sendBroadcast(
new Intent "xper.receiver.intent.RECEIVER_SEA_AREA_INTENT"). setFlags(
Intent.FLAG_RECEIVER_REPLACE_PENDING). putExtra(
"N,
"One sendBroadcast( new Intent
"xper.receiver.intent.RECEIVER_SEA_AREA_INTENT"). setFlags(
Intent.FLAG_RECEIVER_REPLACE_PENDING). putExtra(
"N,
"Two sendBroadcast( new Intent
"xper.receiver.intent.RECEIVER_SEA_AREA_INTENT"). setFlags(
Intent.FLAG_RECEIVER_REPLACE_PENDING). putExtra(
"N,
"Three then we get (output slightly reformatted)
Lundy.onReceive(..., Intent { act=xper.receiver.intent.RECEIVER_SEA_AREA_INTENT flg=0x20000000 has extras) })
Lundy.onReceive(...) N == One
Fastnet.onReceive(..., Intent { act=xper.receiver.intent.RECEIVER_SEA_AREA_INTENT \ flg=0x20000000 cmp=xper.receiver.fastnet/.Fastnet (has extras) })
Fastnet.onReceive(...) N == One
Lundy.onReceive(..., Intent { act=xper.receiver.intent.RECEIVER_SEA_AREA_INTENT flg=0x20000000 has extras) })
Lundy.onReceive(...) N == Two
Lundy.onReceive(..., Intent { act=xper.receiver.intent.RECEIVER_SEA_AREA_INTENT flg=0x20000000 has extras) })
Lundy.onReceive(...) N == Three
Sole.onReceive(..., Intent { act=xper.receiver.intent.RECEIVER_SEA_AREA_INTENT \ flg=0x20000000 cmp=xper.receiver.sole/.Sole (has extras) })
Sole.onReceive(...) N == One
Fastnet.onReceive(..., Intent { act=xper.receiver.intent.RECEIVER_SEA_AREA_INTENT \ flg=0x20000000 cmp=xper.receiver.fastnet/.Fastnet (has extras) })
Fastnet.onReceive(...) N == Three
Sole.onReceive(..., Intent { act=xper.receiver.intent.RECEIVER_SEA_AREA_INTENT \ flg=0x20000000 cmp=xper.receiver.sole/.Sole (has extras) })
Sole.onReceive(...) N == Three
Share with your friends: