Lecture_08x - yimg.com

stuckwarmersΚινητά – Ασύρματες Τεχνολογίες

14 Δεκ 2013 (πριν από 3 χρόνια και 5 μήνες)

166 εμφανίσεις

Mobile Computing

Lecture#08
IntentFilters

&
BroadcastReceivers

Lecture Contents


Intent

Filters

for Plug
-
ins/
Extensibility


Annonymous

Actions to Applications


Intents

to
Broadcast

Events


Listening

for
Broadcasts


Broadcast

Receivers


BroadcastReceivers

in Code


BroadcastReceivers

in XML


Native
Android

Broadcast

Actions




2

Broadcasting


System level message sending mechanism


Structured message sending across applications


Intents can be used to send messages across applications
via
sendBroadcast
() method


Broadcast intents extend the event driven approach (all
applications on a system may behave like event handlers)


Applications registered to handle an event can react to
that event without causing any change in event generating
application



3

Broadcasting an Event

Two step process::::

1.
Build the intent for Broadcast

2.
Broadcast the built intent

4

Android Code

public static final String
new_life_appeared

=

com.test.lives.NEW_LIFE
”;

Intent
intent

= new Intent(
new_life_appeared
);

intent.putExtra
(“type”, “
human_life
”);

intent.putExtra
(“where”, “
unknown_location
”);

……..

……..

sendBroadcast
(intent);

5

Listening for Broadcasts


BroadcastReceiver

is a instance of a class that has
registered itself as receiver for a particular broadcast
event


For a class to work as a
BroadcastRegister
, it must
register itself as
BroadcastReceiver


Two methods to register::

1.
Register in manifest

2.
Register in code

6

Listening for Broadcasts


While registering as a
BroadcastReceiver

a class must
specify the intent
-
filter to describe which event this class
is listening for

7

Creating a
BroadcastReceiver

public class
MyBroadcastReceiver

extends
BroadcastReceiver
{




public void
onReceive
(Context c, Intent
intent
){



//Some code to handle the event


}//End of
onReceive


} //End of
MyBroadcastReceiver

8


onReceive
()

function is called automatically when
event
-
generated is matched with the one described
in intent
-
filter tag.

onReceive
() example

public void
onReceive
(Context
context
, Intent
intent
){

Uri data =
intent.getData
();

String type =
intent.getStringExtra
(“type”);

……

……

Typically launch some activity/service to perform some
action based on the intent received.


}

9

BroadcastReceiver

in XML

<receiver
android:name
=“
com.test.MyBroadcastReceiver
”>


<intent
-
filter>



<action
android:name
=“…….”>



…………………..


</intent
-
filter>

</receiver>

10

Receiver registered

in xml (manifest) will always be
active (even if application is not running/application is
in background)

Receiver Example Code

public void
onReceive
(Context
context
, Intent
intent
){


Bundle extras =
intent.getExtras
();


if (extras != null) {


String state =
extras.getString
(
TelephonyManager.
EXTRA_STATE
);




if
(
state.equals
(
TelephonyManager.
EXTRA_STATE_RINGING
))
{


String
phoneNumber

=
extras.getString
(
TelephonyManager.
EXTRA_INCOMING_NUMBER
);


Log.
i
(tag+":Number",
phoneNumber
);


}


}

}

11

Receiver Example Manifest


<receiver
android:name
=
"
com.braodcast.receiver.MyPhoneReceiver
">


<intent
-
filter>



<action

android:name
=
"
android.intent.action.PHONE_STATE
">



</action>


</intent
-
filter>

</receiver>

12

Registering Receiver in Code

IntentFilter

filter = new
IntentFilter
(
string
-
event
);

MyBroadcastReceiver

receiver = new
MyBroadcastReceiver
();

registerReceiver
(receiver, filter);


13

Unregister a Receiver

unregisterReceiver
(receiver);

14

Dynamic 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.
Listening for outgoing calls during office
-
hours

2.
When phone screen is turned on/off during night

3.
……………



15

Native Android Broadcast Events

ACTION_BOOT_COMPLETED

Fired once when the device has completed
its startup sequence. An

application requires the
RECEIVE_BOOT_COMPLETED
permission to receive this broadcast

ACTION_CAMERA_BUTTON

Fired

when camera button is clicked

ACTION_DATE_CHANGED

ACTION_TIME_CHANGED

Fired when system’s date/time
=
楳⁣桡湧敤=
浡m畡汬y
=
A䍔Cl也䵅䑉䅟䕊䕃b
=

If 瑨攠畳敲u捨c潳敳e瑯t敪散琠瑨攠數瑥牮慬e
獴s牡r攠浥d楡i 瑨楳 ev敮琠楳⁦楲敤⁦楲獴i


If y潵爠a灰汩p慴楯渠楳⁲敡摩湧爠睲楴楮朠瑯 瑨攠
數瑥牮慬敤楡e獴s牡r攠y潵o獨s畬搠汩l瑥渠f潲o瑨楳
ev敮琠楮i潲d敲e瑯t獡v攠慮a 捬c獥s慮礠潰敮yf楬攠
桡湤汥l.

16

Native Android Broadcast Events

ACTION_MEDIA_MOUNTED
ACTION_MEDIA_UNMOUNTED

These two events are broadcast

whenever new external storage media are
successfully added to or removed from the
device.

ACTION_NEW_OUTGOING_CALL

Broadcast when a new outgoing call is about to be
placed. Listen for this broadcast to intercept
outgoing calls.

ACTION_SCREEN_OFF

ACTION_SCREEN_ON

Broadcast when the screen turns off or on

Respectively.

ACTION_TIMEZONE_CHANGED

This action is broadcast whenever the phone’s
捵cr敮琠瑩t攠穯湥n捨慮来c.=周T⁉湴敮琠楮i汵摥猠愠
瑩te
-
穯湥n數瑲愠瑨慴er整畲湳e瑨攠Ia=潦=瑨攠湥眠
java.util.TimeZone
.

17

Pending Intents


The
PendingIntent

class provides a mechanism for
creating Intents that can be fired by another
applicationat

a later time.


A 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 Notification being selected
from the notification panel.


PendingIntent

class offers static methods to construct
Pending Intents used to start an Activity, start a Service,
or broadcast an Intent.

18

Pending Intents

// Start an Activity

Intent
startIntent

= new Intent(this,
OtherActivity.class
);

PendingIntent.getActivity
(this, 0,
startIntent

, 0);

// Broadcast an Intent

Intent
broadcastIntent

= new
Intent(NEW_LIFEFORM_DETECTED);

PendingIntent.getBroadcast
(this, 0,
broadcastIntent
, 0);

19

Pending Intent

It is a token that you give to a foreign application (e.g.
Notification Manager, Alarm Manager, Home Screen
AppWidget

Manager, or other 3rd party applications),
which
allows a foreign application to use your
application's permissions to execute a predefined
piece of code
.

20

Pending Intent

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.


21

Pending Intent Example (Main.xml)

<?xml version="1.0" encoding="utf
-
8"?>



<
LinearLayout

xmlns:android
=…

android:orientation
="vertical“

android:layout_width
="
fill_parent


android:layout_height
="
fill_parent
">


<
EditText


android:layout_height
="
wrap_content
"

android:id
="@+id/time"


android:layout_width
="
wrap_content
"


android:hint
="Number of seconds"


android:inputType
="
numberDecimal
"/>



22

<Button


android:text
="Start Counter"


android:id
="@+id/ok"


android:onClick
="
startAlert
"


android:layout_width
="
wrap_content
"


android:layout_height
="
wrap_content



/>

</
LinearLayout
>


Pending Intent Example (TimerReceiver.java)

public class
TimerReceiver

extends
BroadcastReceiver

{


public void
onReceive
(Context
context
, Intent
intent
){


Toast.makeText
(context, “Your time is up",
Toast.LENGTH_LONG
).show();


// Vibrate the mobile phone


Vibrator
vibrator

= (Vibrator)
context.getSystemService
(
Context.VIBRATOR_SERVICE
);


vibrator.vibrate
(2000);



}

}



23

Pending Intent Example (Main.java)

public class Main extends Activity {


EditText

time;



/** Called when the activity is first created. */


@Override


public void
onCreate
(Bundle
savedInstanceState
) {


super.onCreate
(
savedInstanceState
);


setContentView
(
R.layout.main
);


}


24

Pending Intent Example (Manifest)

<receiver
android:name
=
".
TimerReceiver
">

</receiver>


<uses
-
permission
android:name
=
"
android.permission.VIBRATE
">

</uses
-
permission>

25