Professional Documents
Culture Documents
Android Application Development: Broadcast Receiver
Android Application Development: Broadcast Receiver
Broadcast Receiver
Application Building Blocks Android Component
• UI Component Typically
Activity Corresponding to one screen.
Activities Services
1. Provides User Interface 1. No User Interface
2. Usually represents a Single 2. Runs in Background
Screen
3. Extends the Service Base Class
3. Can contain one/more Views
4. Extends the Activity Base class
Activity 1 Activity 2
UI
Service BroadcastReceiver
Intents
1. Directed Intents
2. Broadcast Intents
OS
BIG PICTURE
Android Application Anatomy Big Picture
Activity
OS BroadcastReceiver
Broadcast Receivers SMS Application
Parameters Descriptions
destinationAddress String: the address to send the message to
scAddress String: is the service center address or null to use the
current default SMSC
text String: the body of the message to send
sentIntent PendingIntent: if not NULL this PendingIntent is broadcast
when the message is successfully sent, or failed. The result
code will be Activity.RESULT_OK for success, or one of
these
errors: RESULT_ERROR_GENERIC_FAILURERESULT_ERROR_
RADIO_OFF RESULT_ERROR_NULL_PDU
deliveryIntent PendingIntent: if not NULL this PendingIntent is broadcast
when the message is delivered to the recipient. The raw
pdu of the status report is in the extended data ("pdu").
Registering BroadcastReceivers Two ways to register
1. Statically
AndroidManifest.XML
2. Dynamically
Context.registerReceiver()
Static Registration Two ways to register
<receiver
android:name=".IncomingSms"
android:enabled = "true">
<intent-filter
android:priority="1000">
<action android:name=
"android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
Dynamic Registration Dynamic
1. Create an IntentFilter
2. Create a BroadcastReceiver
3. Register BroadcastReceiver to receive Intents
using Context.registerReceiver()
4. When appropriate call
Context.unRegisterReceiver() to unRegister
BroadcastReceiver
Dynamic Registration Dynamic
…
@Override
protected void onDestroy() {
unregisterReceiver(br1);
super.onDestroy();
}
…
Event Broadcast Broadcast Events
Parameters Descriptions
Intent Intent: The Intent to broadcast; all receivers matching this
Intent will receive the broadcast
public class
CompoundOrderedBroadcastWithResultReceiver extends
Activity {
…
public static final String CUSTOM_INTENT =
"course.examples.BroadcastReceiver.intent.action.TEST4";
public void onCreate(Bundle savedInstanceState) {
…
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
@Override public void onClick(View v) {
sendOrderedBroadcast(new Intent(CUSTOM_INTENT), null, new
BroadcastReceiver() { @Override public void
onReceive(Context context, Intent intent)
{ System.out.println("Final Result is:" +
getResultData()); } }, null, 0, null, null); } });
Sticky Broadcasts Sticky
Passing results
…
If (intent.getAction().equals(
Intent.ACTION_BATTERY_CHANGED)) {
String age = "Reading taken recently";
if (isInitialStickyBroadcast()) {
age = "Reading may be stale";
}
state.setText("Current Battery Level" +
String.valueOf(intent.getIntExtra(
BatteryManager.EXTRA_LEVEL, -1)) + "\n"
+ age);
}
}
}, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
…