首页 > 代码库 > 5.User Interface/Notifications
5.User Interface/Notifications
1.Notifications
A notification is a message you can display to the user outside of your application‘s normal UI. When you tell the system to issue a
notification, it first appears as an icon in the notification area. To see the details of the notification, the user opens the notification
drawer.
<1>Notifications in the notification area
<2>Noticafications in the notification drawer
2. Notification Display Elements
<1>Normal View
1 ---> Content title
2 ---> Large icon
3 ---> Content text
4 ---> Content info
5 ---> Small icon
6 ---> Time that the notification was issued. set an explicit value with setWhen()
if you don`t if defaults to the time that the system received the notification
<2>Big View
A notification‘s big view appears only when the notification is expanded,
which happens when the notification is at the top of the notification drawer, or when the user expands the notification with a gesture.
Expanded notifications are available starting with Android 4.1.
3. Creating a Notification
3.1 Required notification contents
must contain the following: icon(setSmallIcon()), title(setContentTitle()), text(setContentText())
3.2 Notification priority
The priority acts as a hint to the device UI about how the notification should be displayed.
setPriority() pass in one of the NotificationCompat priority constants.
there are five priority levels, ranging from PRIORITY_MIN(-2) to PRIORITY_MAX(2), if not set, defaults to PRIORITY_DEAFAULT(0)
3.3 Creating a simple notification
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_notification_test); Builder mBuilder = new Notification.Builder(this) .setSmallIcon(R.drawable.mail) .setContentTitle("Mymail") .setContentText("Hello World!")
.setTicker("You have an new message"); NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); //Id allows you to update the notification later on. mNotificationManager.notify( 0, mBuilder.build()); }
3.4 Big View style notification
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); InboxStyle inboxStyle = new InboxStyle(); inboxStyle.setBigContentTitle("5 news messages"); inboxStyle.addLine("message 1"); inboxStyle.addLine("message 2"); inboxStyle.addLine("message 3"); inboxStyle.addLine("message 4"); inboxStyle.addLine("message 5"); Builder mBuilder = new Notification.Builder(this) .setSmallIcon(R.drawable.mail) .setContentTitle("Mymail") .setContentText("Hello World!") .setTicker("You have an new message") .setPriority(Notification.PRIORITY_DEFAULT) .setDefaults(Notification.DEFAULT_VIBRATE) .setStyle(inboxStyle); //Id allows you to update the notification later on. mNotificationManager.notify( 0, mBuilder.build());
4. Manageing Notifications
4.1 Updating notifications
To set up a notification so it can be updated, issue it with a notification ID by calling NotificationManager.notify(ID,notification)
To update this notification once you‘ve issued it, issue the Notification with the same ID you used previously.
if the previous notification is still visible, the system updates it. otherwise, create a new notification instead.
4.2 Removing notifications
dismiss the notification either individually or "clear all"(if notification can be cleared) by calling cancel() and cancelAll()
user clicks the notification, you can call setAutoCancel when created the notification
5. Preserving Navigation when Starting an activity
two general situations:
<1>Regular activity
Notifications from the Gmail app demonstrate this.
When you click a notification for a single email message, you see the message itself. Touching Back takes you backwards through
Gmail to the Home screen, just as if you had entered Gmail from the Home screen rather than entering it from a notification.
You are starting an Activity that‘s part of the application‘s normal workflow. In this situation, set up the PendingIntent to start a
fresh task, and provider the PendingIntent with a back stack thta reproduces the application`s normal BACK behavior
<2>Special activity
The user only sees this Activity if it is started from a notification
5.1 Setting up a regular activity PendingIntent
6. Displaying Progress in aNotification
an animated progress indicator that shows the status of an ongoing operation
determanate form of the indicator (a progress bar)
indeterminate form of the indicator (an activity indicator)
6.1 a fixed-duration progress indicator
private Builder mBuilder; private NotificationManager mNotificationManager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_notification_test); mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); mBuilder = new Notification.Builder(this) .setSmallIcon(R.drawable.mail) .setContentTitle("Picture Download") .setContentText("Download in progress"); new Thread( new Runnable() { @Override public void run() { for (int i = 0; i < 100; i++) { // Sets the progress indicator to a max value, the current completion percentage, // and "determinate" state mBuilder.setProgress(100, i, false); mNotificationManager.notify(1, mBuilder.build()); try { Thread.sleep(5*100); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } // When the loop is finished, updates the notification mBuilder.setContentText("Download complete") // Removes the progress bar .setProgress(0,0,false); mNotificationManager.notify(1, mBuilder.build()); } } ).start(); }
6.2 a continuing activity indicator
mBuilder.setProgress(100, i, false);
http://blog.csdn.net/vipzjyno1/article/details/25248021
5.User Interface/Notifications