Android Interview Questions and Answers What is android?


Broadcast Intents And Intent Flags



Download 0.77 Mb.
View original pdf
Page61/62
Date17.12.2020
Size0.77 Mb.
#54986
1   ...   54   55   56   57   58   59   60   61   62
Android Interview Questions
10. Broadcast Intents And Intent Flags
There are two Intent class constants which define flags specifically for use with broadcast Intents.
10.1 FLAG_RECEIVER_REGISTERED_ONLY

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

Download 0.77 Mb.

Share with your friends:
1   ...   54   55   56   57   58   59   60   61   62




The database is protected by copyright ©ininet.org 2024
send message

    Main page