9.6.1 Registration When a BroadcastReceiver is registered dynamically using the short-form registerReceiver()
method, then, if the associated IntentFilter matches one or more sticky broadcast Intents one of the matching Intents will be returned by the method, and then, at some point the BroadcastReceiver’s onReceive() method will be invoked for each matching sticky
broadcast Intent For example, if we define the class StickyBroadcastReceiver as follows public class StickyBroadcastReceiver extends
BroadcastReceiver
{
Override public void onReceive(Context context, Intent intent)
{
System.out.println("StickyBroadcastReceiver.onReceive(...)");
System.out.println(intent);
System.out.println("\tisInitialStickyBroadcast() => " + isInitialStickyBroadcast());
System.out.println("StickyBroadcastReceiver.onReceive(...) done
}
}
then the following code Intent i = registerReceiver( new StickyBroadcastReceiver(), new IntentFilter(
ConnectivityManager.CONNECTIVITY_ACTION));
System.out.println("registerReceiver() => " + i prints registerReceiver() => Intent { act=android.net.conn.CONNECTIVITY_CHANGE flg=0x10000000 (has extras) } and then the onReceive() method prints
StickyBroadcastReceiver.onReceive(...)
Intent { act=android.net.conn.CONNECTIVITY_CHANGE flg=0x10000000 (has extras) } isInitialStickyBroadcast() => true
StickyBroadcastReceiver.onReceive(...) done When a BroadcastReceiver is registered dynamically using the long-form registerReceiver()
method then, if the associated IntentFilter matches one or more sticky broadcast Intents, the exact behaviour depends on whether or not a permission is specified.
The method will return one of the matching Intents irrespective of whether or not a permission was specified. However,
once the method has returned, if a permission was specified, then the BroadcastReceiver’s onReceive() method will not be invoked on any sticky broadcast Intent sent by an Application which has not been granted that permission. For example,
modifying the previous example, then the following code
Intent i = registerReceiver( new StickyBroadcastReceiver(), new IntentFilter(
ConnectivityManager.CONNECTIVITY_ACTION),
"xper.permission.NO_SUCH_PERMISSION", null prints registerReceiver() => Intent { act=android.net.conn.CONNECTIVITY_CHANGE flg=0x10000000 (has extras) } and that is it. The onReceive() method is not invoked. The behaviour with respect to the onReceive() method is consistent
with the way permissions work, it is really the behaviour of the registerReceiver() method which is anomalous.
Share with your friends: