0% found this document useful (0 votes)
10 views40 pages

FU Lecture 14

The document provides an overview of Broadcast Receivers in Android, which respond to system-wide broadcast announcements such as incoming messages or battery status. It details how to add and register a Broadcast Receiver in the AndroidManifest.xml file and programmatically, as well as how to handle received broadcasts within the onReceive() method. Additionally, it emphasizes the importance of unregistering the Broadcast Receiver to prevent memory leaks when the associated activity is paused or destroyed.

Uploaded by

Chị Anh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views40 pages

FU Lecture 14

The document provides an overview of Broadcast Receivers in Android, which respond to system-wide broadcast announcements such as incoming messages or battery status. It details how to add and register a Broadcast Receiver in the AndroidManifest.xml file and programmatically, as well as how to handle received broadcasts within the onReceive() method. Additionally, it emphasizes the importance of unregistering the Broadcast Receiver to prevent memory leaks when the associated activity is paused or destroyed.

Uploaded by

Chị Anh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 40

Messaging & Networking

Broadcast Receiver

Broadcast Receiver
Agenda
 Broadcast Receiver

 Messaging (SMS Receiver)


Broadcast Receiver

• —a component that responds to system wide


broadcast announcements
o —Incoming SMS, MMS, Email, phone call
o Battery low, Screen has turned off, or a picture was
captured.
• —think of a Broadcast receiver as a “gateway” to
the other components.
• intended to very little work
o Depending on the incoming event to the receiver, it
could start a Service or Activity.
Broadcast Receiver
To add a new Broadcast Receiver
• open the AndroidManifest.xml file
• click on the Application tab at the bottom of the window
• under the Application Nodes section, click Add ...
• select Receiver
• in the Attributes for Receiver section, click on the
Name* link
• Enter the name of your Broadcast Receiver
o e.g. "MyBroadcastReceiver"

• Finish
Broadcast Receiver - Manifest File
If you view AndroidManifest.xml in the XML editor, you
should see something like this

<receiver
android:name="MyBroadcastReceiver"></receiver>
Broadcast Receiver
Your new Broadcast Receiver file should look something like this

public class MyBroadcastReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {

}
}
Broadcast Receiver

Event listener for when


public class MyBroadcastReceiver
a broadcast has been extends BroadcastReceiver {
received.

@Override
public void onReceive(Context context, Intent intent) {

}
}
Broadcast Receiver

How do you know which


public class MyBroadcastReceiver
broadcasts to listen for?
extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {

}
}
Registering BroadcastReceiver
There are two ways to register a BroadcastReceiver to
listen for a broadcast

1 . Adding an <intent-filter> to the Manifest file

2. Programmatically
Registering BroadcastReceiver
1. To add an <intent-filter> to the Manifest and listen for a
Broadcast ...

1. Open AndroidManifest.xml and click on the Application Tab at the bottom


of the Window
2. Select your BroadcastReceiver
3. Click Add ...
4. Make sure "Create new element in the selected element ... " is selected,
then choose Intent Filter
5. Click Add ...
6. Choose Action
7. Select the appropriate permission in the Name drop-down list
a. e.g. android.intent.action.TIME_SET
8. Save AndroidManifest.xml
Registering BroadcastReceiver
Alternatively, if you view AndroidManifest.xml in the XML
editor, you should see something like this

<receiver android:name="MyBroadcastReceiver">

</receiver>
Registering BroadcastReceiver
We need to tell the system which broadcast(s) we want to
listen for. First we need to add an <intent-filter> tag

<receiver android:name="MyBroadcastReceiver">

</receiver>
Registering BroadcastReceiver
We need to tell the system which broadcast(s) we want to
listen for. First we need to add an <intent-filter> tag

Place the cursor here


and press
Ctrl + Space
<receiver android:name="MyBroadcastReceiver">

</receiver>
Registering BroadcastReceiver
We need to tell the system which broadcast(s) we want to
listen for. First we need to add an <intent-filter> tag

Select <intent-filter>

<receiver android:name="MyBroadcastReceiver">

</receiver>
Registering BroadcastReceiver
We need to tell the system which broadcast(s) we want to
listen for. First we need to add an <intent-filter> tag

<receiver android:name="MyBroadcastReceiver">
<intent-filter>

</intent-filter>
</receiver>
Registering BroadcastReceiver
We need to tell the system which broadcast(s) we want to
listen for. First we need to add an <intent-filter> tag

an <intent-filter>
<receiver android:name="MyBroadcastReceiver">
describes what this
receiver should listen for
<intent-filter>

</intent-filter>
</receiver>
Registering BroadcastReceiver
We need to tell the system which broadcast(s) we want to
listen for. First we need to add an <intent-filter> tag

Place the cursor here


<receiver
and press android:name="MyBroadcastReceiver">
Ctrl + Space
<intent-filter>

</intent-filter>
</receiver>
Registering BroadcastReceiver
We need to tell the system which broadcast(s) we want to
listen for. First we need to add an <intent-filter> tag

<receiver android:name="MyBroadcastReceiver">
Select <action/>
<intent-filter>

</intent-filter>
</receiver>
Registering BroadcastReceiver
We need to tell the system which broadcast(s) we want to
listen for. First we need to add an <intent-filter> tag

Press Ctrl + Space


<receiver android:name="MyBroadcastReceiver">
again
<intent-filter>
<action />
</intent-filter>
</receiver>
Registering BroadcastReceiver
We need to tell the system which broadcast(s) we want to
listen for. First we need to add an <intent-filter> tag

<receiver android:name="MyBroadcastReceiver">
Select android:name

<intent-filter>
<action />
</intent-filter>
</receiver>
Registering BroadcastReceiver
We need to tell the system which broadcast(s) we want to
listen for. First we need to add an <intent-filter> tag

Let's try receiving a


broadcast for when the
<receiver android:name="MyBroadcastReceiver">
screen has been turned
<intent-filter> on

<action android:name=" "/>


</intent-filter>
</receiver>
Registering BroadcastReceiver
We need to tell the system which broadcast(s) we want to
listen for. First we need to add an <intent-filter> tag

Press Ctrl + Space


<receiver android:name="MyBroadcastReceiver">
again
<intent-filter>
<action android:name=" "/>
</intent-filter>
</receiver>
Registering BroadcastReceiver
We need to tell the system which broadcast(s) we want to
listen for. First we need to add an <intent-filter> tag

<receiver android:name="MyBroadcastReceiver">
Select android.intent.action.TIME_SET

<intent-filter>
<action android:name=" "/>
</intent-filter>
</receiver>
Registering BroadcastReceiver
We need to tell the system which broadcast(s) we want to
listen for. First we need to add an <intent-filter> tag

<receiver android:name="MyBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.TIME_SET"/>
</intent-filter>
</receiver>
Registering BroadcastReceiver
We need to tell the system which broadcast(s) we want to
listen for. First we need to add an <intent-filter> tag

<receiver android:name="MyBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.TIME_SET"/>
</intent-filter>
</receiver>
Registering BroadcastReceiver

2. To register a BroadcastReceiver programmatically ...


Registering BroadcastReceiver
public class MainActivity extends Activity {

MyBroadcastReceiver receiver;

@Override public void onCreate(Bundle savedInstanceState) {


super.onCreate(savedInstanceState);
setContentView(R.layout.main);

receiver = new MyBroadcastReceiver();

IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);

registerReceiver(receiver, filter);
}
}
Registering BroadcastReceiver
public class MainActivity extends Activity {

MyBroadcastReceiver receiver;

@Override public void onCreate(Bundle savedInstanceState) {


super.onCreate(savedInstanceState);
Create a new instance
setContentView(R.layout.main);
of your receiver

receiver = new MyBroadcastReceiver();

IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);

registerReceiver(receiver, filter);
}
}
Registering BroadcastReceiver
public class MainActivity extends Activity {

MyBroadcastReceiver receiver;

@Override public void onCreate(Bundle savedInstanceState) {


super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Create an intent filter


receiver = new MyBroadcastReceiver();

IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);

registerReceiver(receiver, filter);
}
}
Registering BroadcastReceiver
public class MainActivity extends Activity {

MyBroadcastReceiver receiver;

@Override public void onCreate(Bundle savedInstanceState) {


super.onCreate(savedInstanceState);
setContentView(R.layout.main);

receiver = new MyBroadcastReceiver();

The receiver is now


IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
registered

registerReceiver(receiver, filter);
}
}
BroadcastReceiver - onReceive() Now let's look at our
BroadcastReceiver

public class MyBroadcastReceiver extends BroadcastReceiver {


@Override
public void onReceive(Context context, Intent intent) {

Intent myIntent = new Intent(context,


ActivityToBeLaunched.class);

Toast.makeText(
context,
"Broadcast Received!",
Toast.LENGTH_LONG).show();

myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(myIntent);
}
BroadcastReceiver - onReceive()
public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
At this point, the
public void onReceive(Context
broadcast has been context, Intent intent) {
received

Intent myIntent = new Intent(context,


ActivityToBeLaunched.class);

Toast.makeText(
context,
"Broadcast Received!",
Toast.LENGTH_LONG).show();

myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(myIntent);
}
BroadcastReceiver - onReceive()
public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
Let's say we want to
launch a new Activity
public void onReceive(Context context, Intent intent) {
when we receive the
broadcast

Intent myIntent = new Intent(context,


ActivityToBeLaunched.class);

Toast.makeText(
context,
"Broadcast Received!",
Toast.LENGTH_LONG).show();

myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(myIntent);
}
BroadcastReceiver - onReceive()
public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {

AndIntent
we wantmyIntent
to show a =
new Intent(context,
Toast
ActivityToBeLaunched.class);

Toast.makeText(
context,
"Broadcast Received!",
Toast.LENGTH_LONG).show();

myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(myIntent);
}
BroadcastReceiver - onReceive()
public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {

Intent myIntent = new Intent(context,


ActivityToBeLaunched.class);

Toast.makeText(
context,
You need to add flags
"Broadcast
to your intent this time
Received!",
Toast.LENGTH_LONG).show();

myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(myIntent);
}
BroadcastReceiver - onReceive()
public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {

Intent myIntent = new Intent(context,


ActivityToBeLaunched.class);

Toast.makeText(
context,
Calling startActivity() from outside of
"Broadcast Received!",
an Activity context requires the
FLAG_ACTIVITY_NEW_TASK flag
Toast.LENGTH_LONG).show();

myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(myIntent);
}
Unregistering BroadcastReceiver

Sometimes after you start listening for a Broadcast, you


may want to STOP listening for it at some point

To UNregister a BroadcastReceiver programmatically ...


Unregistering BroadcastReceiver
public class MainActivity extends Activity {

@Override
public void onPause() {

unregisterReceiver(receiver);
super.onPause();

}
}
Unregistering BroadcastReceiver
public class MainActivity extends Activity {

You will want to unregister before


@Override
calling a super.onSomething()
public voidotherwise
method, onPause() { get
you may
Force Close

unregisterReceiver(receiver);
super.onPause();

}
}
Unregistering BroadcastReceiver
If you register a BroadcastReceiver
programmatically, you should unregister it
before your Activity is destroyed!
• otherwise you will have a "leaked" receiver
• Please call unregisterReceiver in
o onPause()
o onStop()
o or onDestroy()
This is important

You might also like