Вы находитесь на странице: 1из 19

{

Y  

Intents, Intent-Filters

’ecture#07
J

’   

VIntent Filters for Plug-ins/Extensibility


VAnnonymous Actions to Applications
VIntents to Broadcast Events
V’istening for Broadcasts
VBroadcast Receivers
VBroadcastReceivers in Code
VBroadcastReceivers in XM’
V ative Android Broadcast Actions


Ê

 
Vxystem level message sending mechanism
Vxtructured message sending across applications
VIntents can be used to send messages across
applications via sendBroadcast() method
VBroadcast intents extend the event driven
approach (all applications on a system may
behave like event handlers)
VApplications registered to handle an event can
react to that event without causing any change in
event generating application
0

Ê

  
 
mwo step process::::
1. Build the intent for Broadcast
2. Broadcast the built intent
ÿ

Õ  
public static final xtring new_life_appeared =
³´com.test.lives. EW_’IFE;
Intent intent = new Intent(new_life_appeared);
intent.putExtra(³type´, ³human_life´);
intent.putExtra(³where´, ³unknown_location´);
««..
««..
sendBroadcast(intent);


’    Ê


VBroadcastReceiver is a instance of a class that
has registered itself as receiver for a particular
broadcast event
VFor a class to work as a BroadcastRegister, it
must register itself as BroadcastReceiver
Vmwo methods to register::
1. Register in manifest
2. Register in code
î

’    Ê


VWhile registering as a BroadcastReceiver a class
must specify the intent-filter to describe which
event this class is listening for
D


  
Ê

   
public class MyBroadcastReceiver extends BroadcastReceiver{

public void onReceive(Context c, Intent intent){


//xome code to handle the event
}//End of onReceive

} //End of MyBroadcastReceiver

VonReceive() function is called automatically


when event-generated is matched with the one
described in intent-filter tag.
·

    

public void onReceive(Context context, Intent intent){
Uri data = intent.getData();
xtring type = intent.getxtringExtra(³type´);
««
««
mypically launch some activity/service to perform
some action based on the intent received.

}
{

Ê

    ’
-receiver
android:name=³com.test.MyBroadcastReceiver´>
-intent-filter>
-action android:name=³««.´>
«««««««..
-/intent-filter>
-/receiver>
Receiver registered in xml (manifest) will always
be active (even if application is not
running/application is in background)
{{

        
IntentFilter filter = new IntentFilter(string-event);
MyBroadcastReceiver receiver = new
MyBroadcastReceiver();
registerReceiver(receiver, filter);
{J

‰  
   

unregisterReceiver(receiver);
{

  
    
A receiver can register as a receiver for any global
event for a particular period of time and later
can unregister when span of interest is gone.
1. ’istening for outgoing calls during office-hours
2. When phone screen is turned on/off during
night
3. «««««
{0


  Õ  Ê

 
Õ  

 
  Õ
   
!

"
 


    #
ACmIO _CAMERA_BUmmO Fired when camera button is clicked
ACmIO _DAmE_CHA ED Fired when system¶s date/time is changed
ACmIO _mIME_CHA ED manually
ACmIO _MEDIA_EJECm ´If the user chooses to eject the external
storage media, this event is fired first.
´If your application is reading or writing to
the external media storage you should listen
for this event in order to save and close any
open file handles.
{ÿ


  Õ  Ê

 
Õ 
Õ$
 #
Õ 
Õ$$
%& 
'(
' 
ACmIO _ EW_OUmOI _CA’’ Broadcast when a new outgoing call is about to
be placed. ’isten for this broadcast to intercept
outgoing calls.
ACmIO _xCREE _OFF Broadcast when the screen turns off or on
ACmIO _xCREE _O Respectively.
ACmIO _mIMEZO E_CHA ED mhis action is broadcast whenever the phone¶s
current time zone changes. mhe Intent includes
a time-zone extra that returns the ID of the
new java.util.mimeZone.
{

6    
Vmhe PendingIntent class provides a mechanism
for creating Intents that can be fired by another
applicationat a later time.
VA Pending Intent is commonly used to package
an Intent that will be fired in response to a
future event, such as a widget View being clicked
or a otification being selected from the
notification panel.
‡ PendingIntent class offers static methods to
construct Pending Intents used to start an
Activity, start a xervice, or broadcast an Intent.

6    
// xtart an Activity
Intent startIntent = new Intent(this, OtherActivity.class);
PendingIntent.getActivity(this, 0, startIntent , 0);
// Broadcast an Intent
Intent broadcastIntent = new
Intent( EW_’IFEFORM_DEmECmED);
PendingIntent.getBroadcast(this, 0, broadcastIntent, 0);
{D

6    
It is a token that you give to a foreign application
(e.g. otification Manager, Alarm Manager,
Home xcreen AppWidget Manager, or other 3rd
party applications), which ' &
  (  )
  %'  
'.

6    
If you give the foreign application an Intent, and
that application sends/broadcasts the Intent you
gave, they will execute the Intent with their own
permissions. But if you instead give the foreign
application a Pending Intent you created using
your own permission, that application will
execute the contained Intent using your
application's permission.

Вам также может понравиться