Академический Документы
Профессиональный Документы
Культура Документы
Dynamic:
Use Context.registerReceiver () method to dyna
mically register an instance.
Classes of Broadcast
The two major classes of broadcasts are:
Ordered Broadcasts:
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.
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);
}
}