Académique Documents
Professionnel Documents
Culture Documents
Table of Contents
1. Broadcast receiver
1.1. Definition
1.2. Implementation
1.3. Life cycle of a broadcast receiver
1.4. Asynchronous processing
1.5. Restrictions for defining broadcast receiver
1.6. Send the broadcast to your application for testing
2. System broadcasts
3. Automatically starting Services from a Receivers
4. Pending Intent
5. Exercise: Register a receiver for incoming phone calls
5.1. Target
5.2. Create project
5.3. Implement receiver for the phone event
5.4. Request permission
5.5. Validate implementations
6. Exercise: System services and receiver
6.1. Target|
6.2. Implement project
6.3. Validate implementation
7. Defining custom events and receivers
7.1. Registering broadcast receiver for custom events
7.2. Sending broadcast intents
7.3. Local broadcast events with LocalBroadcastManager
8. Dynamic broadcast receiver registration
converted by Web2PDFConvert.com
1. Broadcast receiver
1.1. Definition
A broadcast receiver (short receiver) is an Android component which allows you to register for system or application events. All registered
receivers for an event are notified by the Android runtime once this event happens.
For example, applications can register for the ACTION_BOOT_COMPLETED system event which is fired once the Android system has
completed the boot process.
1.2. Implementation
A receiver can be registered via the AndroidManifest.xml file.
Alternatively to this static registration, you can also register a receiver dynamically via the Context.registerReceiver() method.
The implementing class for a receiver extends the BroadcastReceiver class.
If the event for which the broadcast receiver has registered happens, the onReceive() method of the receiver is called by the Android
system.
Tip: This does not mean the user has to start the application again after a reboot. The Android system remembers that
the user already started it. Only one start is required without a forced stop by the user.
converted by Web2PDFConvert.com
2. System broadcasts
Several system events are defined as final static fields in the Intent class. Other Android system classes also define events, e.g., the
TelephonyManager defines events for the change of the phone state.
The following table lists a few important system events.
Table 1. System Events
Event
Description
Intent.ACTION_BOOT_COMPLETED
Intent.ACTION_POWER_CONNECTED
Intent.ACTION_POWER_DISCONNECTED
Intent.ACTION_BATTERY_LOW
Intent.ACTION_BATTERY_OKAY
converted by Web2PDFConvert.com
@Override
public void onReceive(Context context, Intent intent) {
Tip: If your application is installed on the SD card, then it is not available after the
android.intent.action.BOOT_COMPLETED event. In this case register it for the
android.intent.action.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE event.
Note: Remember that as of Android API level 11 the user needs to have started the application at least once before your
application can receive android.intent.action.BOOT_COMPLETED events.
4. Pending Intent
A pending intent is a token that you give to another application (e.g., notification manager, alarm manager or other 3rd party applications),
which allows this other application to use the permissions of your application to execute a predefined piece of code.
converted by Web2PDFConvert.com
To perform a broadcast via a pending intent, get a PendingIntent via the getBroadcast() method of the PendingIntent class. To
perform an activity via a pending intent, you receive the activity via PendingIntent.getActivity() .
Tip: Remember that your receiver is only called if the user started it once. This requires an activity.
5.3. Implement receiver for the phone event
Create the MyPhoneReceiver class.
package de.vogella.android.receiver.phone;
import
import
import
import
import
import
android.content.BroadcastReceiver;
android.content.Context;
android.content.Intent;
android.os.Bundle;
android.telephony.TelephonyManager;
android.util.Log;
@Override
public void onReceive(Context context, Intent intent) {
Bundle extras = intent.getExtras();
if (extras != null) {
String state = extras.getString(TelephonyManager.EXTRA_STATE);
Log.w("MY_DEBUG_TAG", state);
if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
String phoneNumber = extras
.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
Log.w("MY_DEBUG_TAG", phoneNumber);
}
}
}
}
converted by Web2PDFConvert.com
</manifest>
converted by Web2PDFConvert.com
android.content.BroadcastReceiver;
android.content.Context;
android.content.Intent;
android.os.Vibrator;
android.widget.Toast;
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Don't panik but your time is up!!!!.",
Toast.LENGTH_LONG).show();
converted by Web2PDFConvert.com
converted by Web2PDFConvert.com
package de.vogella.android.alarm;
import
import
import
import
import
import
import
import
android.app.Activity;
android.app.AlarmManager;
android.app.PendingIntent;
android.content.Intent;
android.os.Bundle;
android.view.View;
android.widget.EditText;
android.widget.Toast;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void startAlert(View view) {
EditText text = (EditText) findViewById(R.id.time);
int i = Integer.parseInt(text.getText().toString());
Intent intent = new Intent(this, MyBroadcastReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 234324243, inte
nt, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()
+ (i * 1000), pendingIntent);
Toast.makeText(this, "Alarm set in " + i + " seconds",
Toast.LENGTH_LONG).show();
}
}
converted by Web2PDFConvert.com
converted by Web2PDFConvert.com
@Override
public void onResume() {
super.onResume();
@Override
public void onReceive(Context context, Intent intent) {
@Override
protected void onPause() {
// add data
intent.putExtra("message", "data");
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}
Context.unregisterReceiver()
method. If you forget this, the Android system reports a leaked broadcast receiver error. For instance, if you
registered a receive in onResume() methods of your activity, you should unregister it in the onPause() method.
converted by Web2PDFConvert.com
converted by Web2PDFConvert.com
TRAINING
converted by Web2PDFConvert.com