Android Interview Questions and Answers What is android?


Accessing A Sticky Broadcast Intent Directly



Download 0.77 Mb.
View original pdf
Page58/62
Date17.12.2020
Size0.77 Mb.
#54986
1   ...   54   55   56   57   58   59   60   61   62
Android Interview Questions
9.4 Accessing A Sticky Broadcast Intent Directly
Both registerReceiver() methods can be passed a receiver argument of null. As in the non-null receiver argument case if one or more sticky broadcast Intents match the supplied IntentFilter then one of them will be returned from the method. For example, although it is not documented as such the broadcast Intent with the action android.net.conn.CONNECTIVITY_CHANGE is sent in sticky mode and hence is accesible in this way. On the emulator the following code
IntentFilter f = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);


Intent i = registerReceiver(null, f
System.out.println(i);
Bundle b = i.getExtras(); for (String key : b.keySet())
{
System.out.print(key);
System.out.print(" => ");
System.out.println(b.get(key));
} prints
Intent { act=android.net.conn.CONNECTIVITY_CHANGE flg=0x10000000 (has extras) } networkInfo => NetworkInfo: type mobile[UMTS], state CONNECTED/CONNECTED, reason simLoaded, ... [elided] reason => simLoaded extraInfo => internet inetCondition => 0 Although it is possible to use the long-form of the registerReceiver()
method in this way there is effectively no point since the permission argument has no effect. For example, replacing the line
Intent i = registerReceiver(null, fin the example above with
Intent i = registerReceiver(null, f, "xper.permission.NO_SUCH_PERMISSION", null does not change the behaviour at all.

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