編寫:wangyachen - 原文: http://developer.android.com/training/wearables/notifications/stacks.html
當(dāng)為手持式設(shè)備創(chuàng)建Notification時(shí),開發(fā)者應(yīng)該將多個(gè)相似的Notification合并成一個(gè)概括式的Notification。例如,如果app創(chuàng)建了一系列接收短信的Notification,開發(fā)者不應(yīng)該將多于一個(gè)Notification顯示到可穿戴設(shè)備上——當(dāng)接收到多于一條消息的時(shí)候,用一個(gè)Notification提供一個(gè)摘要,比如"2條新消息"。
盡管如此,一個(gè)概括式的Notification在可穿戴設(shè)備上并不是很有用處,因?yàn)橛脩舨荒茉诳纱┐髟O(shè)備上閱讀每條消息的詳細(xì)內(nèi)容(他們必須在手持式設(shè)備上打開相應(yīng)的app才能看到更多信息)。所以對可穿戴設(shè)備而言,開發(fā)者應(yīng)該將所有的Notification都集中起來,放成一疊。這疊Notification以一張卡片的形式顯示出來,用戶可以將它展開,分別看到每個(gè)Notification的詳細(xì)內(nèi)容。通過新方法setGroup()能夠?qū)崿F(xiàn)該功能,同時(shí),也能保持手持式設(shè)備上顯示為一條概括式的Notification。
http://wiki.jikexueyuan.com/project/android-training-geek/images/11_bundles_A.png" alt="" /> http://wiki.jikexueyuan.com/project/android-training-geek/images/11_bundles_B.png" alt="" />
為了創(chuàng)建一個(gè)stack,可以對每個(gè)想要放入該stack的Notification調(diào)用setGroup(),并且指定一個(gè)group key。然后調(diào)用[notify()](http://developer.android.com/reference/java/lang/Object.html#notify())將其發(fā)送至可穿戴設(shè)備上。
final static String GROUP_KEY_EMAILS = "group_key_emails";
// Build the notification, setting the group appropriately
Notification notif = new NotificationCompat.Builder(mContext)
.setContentTitle("New mail from " + sender1)
.setContentText(subject1)
.setSmallIcon(R.drawable.new_mail);
.setGroup(GROUP_KEY_EMAILS)
.build();
// Issue the notification
NotificationManagerCompat notificationManager =
NotificationManagerCompat.from(this);
notificationManager.notify(notificationId1, notif);
稍后,當(dāng)開發(fā)者創(chuàng)建另一個(gè)Notification的時(shí)候,指定同樣的group key。當(dāng)在調(diào)用[notify()](http://developer.android.com/reference/java/lang/Object.html#notify())的時(shí)候,這個(gè)Notification就會(huì)出現(xiàn)在之前那個(gè)Notification的同一個(gè)stack中,而非新建一張卡片。
Notification notif2 = new NotificationCompat.Builder(mContext)
.setContentTitle("New mail from " + sender2)
.setContentText(subject2)
.setSmallIcon(R.drawable.new_mail);
.setGroup(GROUP_KEY_EMAILS)
.build();
notificationManager.notify(notificationId2, notif2);
在默認(rèn)的情況下,Notification的排列順序由開發(fā)者添加的先后順序決定,最近的Notification會(huì)被放置在最頂端。你可以通過setSortKey()來修改Notification的排順序。
在手持設(shè)備上提供一個(gè)概括式的Notification是很重要的。因此除了要將每條單獨(dú)的Notification放置在同一個(gè)stack group中,還需要添加一個(gè)概括式的Notification,并對其調(diào)用setGroupSummary()即可實(shí)現(xiàn)。
該Notification并不會(huì)出現(xiàn)在可穿戴設(shè)備上的stack中,只會(huì)出現(xiàn)在手持式設(shè)備上。
http://wiki.jikexueyuan.com/project/android-training-geek/images/notif_summary_framed.png" alt="" />
Bitmap largeIcon = BitmapFactory.decodeResource(getResources(),
R.drawable.ic_large_icon);
// Create an InboxStyle notification
Notification summaryNotification = new NotificationCompat.Builder(mContext)
.setContentTitle("2 new messages")
.setSmallIcon(R.drawable.ic_small_icon)
.setLargeIcon(largeIcon)
.setStyle(new NotificationCompat.InboxStyle()
.addLine("Alex Faaborg Check this out")
.addLine("Jeff Chang Launch Party")
.setBigContentTitle("2 new messages")
.setSummaryText("johndoe@gmail.com"))
.setGroup(GROUP_KEY_EMAILS)
.setGroupSummary(true)
.build();
notificationManager.notify(notificationId3, summaryNotification);
該Notification使用了NotificationCompat.InboxStyle,這個(gè)style能夠讓開發(fā)者很輕松地創(chuàng)建郵件或者短信app的Notifications。開發(fā)者可以對概括式Notification使用這個(gè)style,或者NotificationCompat中定義的其他style,或者不使用任何style也可以。
Tip:如果想要和上面截圖中一樣的設(shè)計(jì)文本,請參考Styling with HTML markup和Styling with Spannables。
概括式Notification能夠在不顯示在可穿戴設(shè)備上的前提下做到影響可穿戴設(shè)備上的Notification。當(dāng)開發(fā)者創(chuàng)建一個(gè)概括式Notification時(shí),可以利用NotificationCompat.WearableExtender,調(diào)用setBackground()或者addAction()為可穿戴設(shè)備上的整個(gè)stack設(shè)置一個(gè)背景圖片或者一個(gè)action。以下代碼展示了如何為整個(gè)stack設(shè)置背景:
Bitmap background = BitmapFactory.decodeResource(getResources(),
R.drawable.ic_background);
NotificationCompat.WearableExtender wearableExtender =
new NotificationCompat.WearableExtender()
.setBackground(background);
// Create an InboxStyle notification
Notification summaryNotificationWithBackground =
new NotificationCompat.Builder(mContext)
.setContentTitle("2 new messages")
...
.extend(wearableExtender)
.setGroup(GROUP_KEY_EMAILS)
.setGroupSummary(true)
.build();