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

Android Broadcast Receiver

Apa itu Broadcast Receiver?


A dormant component of the Android system
Only an Intent (for which it is registered) can bring it into ac
tion
The Broadcast Receivers job is to pass a notification to the
user, in case a specific event occurs.

Broadcast Receiver Example


Real-time announcements of what is happening
in the Android system and the applications
Notifications like incoming messages, WiFi Activated
/Deactivated message

A Broadcast receiver triggers battery Low notific


ation that you see on your mobile screen .
Facebook app new friend notifications, new frie
nd feeds, new message etc

Why need Content Providers


Support apps primary task
Important for User Interface
User Friendly
Intuitive

Registration of Broadcast Receiver


There are two ways to register a Broadca
st Receiver
Static:
Use <receiver> tag in your Manifest file. (Android
Manifest.xml)

Dynamic:
Use Context.registerReceiver () method to dyna
mically register an instance.

Classes of Broadcast
The two major classes of broadcasts are:
Ordered Broadcasts:

Synchronous, follow a specific order


The order is defined using android: priority attribute
Receivers with greater priority would receive the broadcast first
Same priority = arbitrary order
Each receiver can either pass on the notification to the next one, or abort th
e broadcast completely

Normal Broadcasts:
Normal broadcasts are not orderly. Therefore, the registered receivers often
run all at the same time. This is very efficient, but the Receivers are unable t
o utilize the results.

Sometimes to avoid system overload, the system delivers the


broadcasts one at a time, even in case of normal broadcasts.

Difference with Activity Intent?


Broadcasting Intents are different from th
e Intents used to start an Activity or a Servi
ce
Activity Intent: makes changes to an opera
tion the user is interacting with
user is aware of the process

Broadcasting intent: the operation runs co


mpletely in the background,
invisible to the user (not aware)

Implementing Broadcast Receiver


Create a subclass of Androids BroadcastReceiver
Implement the onReceive() method:
E.g. battery low notification, the receiver is registered to Intent.ACTION_BATTERY_LO
W event. As soon as the battery level falls below the defined level, this onReceive() me
thod is called.

Following are the two arguments of the onReceive() method:


Context: This is used to access additional information, or to start services or activities.
Intent: The Intent object is used to register the receiver.

The Broadcast Receiver object active only for the duration of onReceive (Con
text, Intent).
If you need to allow an action after receiving the notification services should
be triggered, and not broadcast receivers.
To show a dialogue, then you should use NotificationManager API
If you wish to send a broadcast intent that would stick around even after the
broadcast is complete, you must use sendStickyBroadcast (Intent) method.

Example
A notification is generated when you change the syste
m time
When the notification clicked leads the user to the Co
ntacts

Example Code
public class MyBroadcastReceiver extends BroadcastReceiver {
private NotificationManager mNotificationManager;
private int SIMPLE_NOTFICATION_ID;
@Override
public void onReceive(Context context, Intent intent) {
mNotificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notifyDetails = new Notification(R.drawable.android,
"Time Reset!", System.currentTimeMillis());
PendingIntent myIntent = PendingIntent.getActivity(context, 0,
new Intent(Intent.ACTION_VIEW, People.CONTENT_URI), 0);
notifyDetails.setLatestEventInfo(context, "Time has been Reset",
"Click on me to view Contacts", myIntent);
notifyDetails.flags |= Notification.FLAG_AUTO_CANCEL;
notifyDetails.flags |= Notification.DEFAULT_SOUND;
mNotificationManager.notify(SIMPLE_NOTFICATION_ID, notifyDetails);
}
}

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