----------------------------------------------------------------------------------------------------------------------------------------------------------

原文作者:Google

原文地址:http://developer.android.com/wear/notifications/stacks.html

原文版权:Creative Commons 2.5 Attribution License

译文作者:Jianan - qinxiandiqi@foxmail.com

版本信息:本文基于2014-06-24版本翻译

译文版权:CC BY-NC-ND 4.0,允许复制转载,但必须保留译文作者署名及译文链接,不得演绎和用于商业用途

----------------------------------------------------------------------------------------------------------------------------------------------------------


前言


当在手持设备上创建通知的时候,你应该经常将一些类似的通知归并到一个单一的摘要通知中。比如,如果你的应用接收到信息后会创建通知,你不应该在手持设备上创建多条通知。当接收到多条信息的时候,你应该使用一条单一的通知并显示类似“2 new messages”这样的摘要信息。


Android Wear Preview- 归档通知(Stacking Notifications)_第1张图片Android Wear Preview- 归档通知(Stacking Notifications)_第2张图片


但是,一个摘要通知在Android Wear设备上就显得没那么有用,因为用户不能够在穿戴设备上详细阅读每条信息(他们必须在手持设备上打开你的应用程序来查看更多的信息)。因此,在穿戴设备上,你应该将所有通知归档到一个栈中。包含多个通知的栈将作为一张卡片显示,用户可以展开来查看每一条通知的详细信息。新的setGroup()方法让这一切成为可能,并且还能够同时在手持设备上只保持提供一条摘要通知。


更多关于设计通知栈的内容,请参考Design Principles of Android Wear。


Add Each Notification to a Group(将每一条信息分组)


创建一个栈,你需要为每条通知调用setGroup()方法,并指定分组的key。然后调用notfiy()方法将它发送到穿戴设备上。

final static String GROUP_KEY_EMAILS = "group_key_emails";// Build the notification and pass this builder to WearableNotifications.BuilderNotificationCompat.Builder builder = new NotificationCompat.Builder(mContext)         .setContentTitle("New mail from " + sender1)         .setContentText(subject1)         .setSmallIcon(R.drawable.new_mail);Notification notif1 = new WearableNotifications.Builder(builder)         .setGroup(GROUP_KEY_EMAILS)         .build();// Issue the notificationNotificationManagerCompat notificationManager =        NotificationManagerCompat.from(this);notificationManager.notify(notificationId1, notif);

之后,当你创建其它通知的时候,只要你指定相同的分组key。那么你调用notify()方法之后,这条通知就会跟之前的通知一样出现在相同的通知栈里面,并替代成为一张新的卡片:

builder = new NotificationCompat.Builder(mContext)         .setContentTitle("New mail from " + sender2)         .setContentText(subject2)         .setSmallIcon(R.drawable.new_mail);// Use the same group as the previous notificationNotification notif2 = new WearableNotifications.Builder(builder)         .setGroup(GROUP_KEY_EMAILS)         .build();notificationManager.notify(notificationId2, notif);

默认情况,通知的显示顺序由你的添加顺序决定,最近添加的通知将会出现在最顶部。你也可以为通知在栈中指定一个序号,只要你将序号作为setGroup()方法的第二个参数传递进去。


Add a Summary Notification(添加一条摘要通知)


在手持设备上保持提供一条摘要通知是相当重要的。因此,除了将每一条通知添加到相同的栈中之外,还要添加一条摘要通知到栈中,只不过要把摘要通知的序号设置为GROUP_ORDER_SUMMARY。


Android Wear Preview- 归档通知(Stacking Notifications)_第3张图片


这条摘要通知不会出现在穿戴设备上的通知栈中,但是会作为一条通知出现在手持设备上。

Bitmap largeIcon = BitmapFactory.decodeResource(getResources(),        R.drawable.ic_large_icon);builder = new NotificationCompat.Builder(this)        .setSmallIcon(R.drawable.ic_small_icon)        .setLargeIcon(largeIcon);// Use the same group key and pass this builder to InboxStyle notificationWearableNotifications.Builder wearableBuilder = new WearableNotifications        .Builder(builder)        .setGroup(GROUP_KEY_EMAILS,                WearableNotifications.GROUP_ORDER_SUMMARY);// Build the final notification to show on the handsetNotification summaryNotification = new NotificationCompat.InboxStyle(        wearableBuilder.getCompatBuilder())        .addLine("Alex Faaborg   Check this out")        .addLine("Jeff Chang   Launch Party")        .setBigContentTitle("2 new messages")        .setSummaryText("johndoe@gmail.com")        .build();notificationManager.notify(notificationId3, summaryNotification);

这条通知使用了NotificationCompat.InboxStyle,它提供了一种为邮件或者信息类应用程序创建通知的简单方法。你可以采用这种风格,而其它的通知使用NotificationCompat来定义,当然你也可以完全不是用这种风格来定义摘要通知。


提示:定义类似截图中的文字风格,可以参考Styling with HTML markup和Styling with Spannables.

更多相关文章

  1. Android获取设备状态栏status bar高度的正确姿势
  2. FAQ_05_查看 android 设备 ip
  3. Ubuntu下连接Android设备
  4. android:versionCode和android:versionName 用途(转) App自动更
  5. Android与物联网设备通信-概念入门
  6. 获取Android设备基本信息
  7. Android获取OAID设备标识
  8. Android通知的基本用法
  9. android 获取设备型号

随机推荐

  1. mysql 5.7.21 winx64安装配置方法图文教
  2. mysql max 与 where 间的执行问题小结
  3. mysql5.7.21.zip安装教程
  4. mysql 查询当天、本周,本月,上一个月的数
  5. Linux连接mysql报错:Access denied for us
  6. Linux中安装MySql 5.7.21的详细操作步骤
  7. 分析MySQL复制以及调优原理和方法
  8. mysql按照天统计报表当天没有数据填0的实
  9. MySQL数据库的主从同步配置与读写分离
  10. Mysql实验之使用explain分析索引的走向