android通知notification全面剖析

上传人:第*** 文档编号:31319827 上传时间:2018-02-06 格式:DOC 页数:14 大小:237.50KB
返回 下载 相关 举报
android通知notification全面剖析_第1页
第1页 / 共14页
android通知notification全面剖析_第2页
第2页 / 共14页
android通知notification全面剖析_第3页
第3页 / 共14页
android通知notification全面剖析_第4页
第4页 / 共14页
android通知notification全面剖析_第5页
第5页 / 共14页
点击查看更多>>
资源描述

《android通知notification全面剖析》由会员分享,可在线阅读,更多相关《android通知notification全面剖析(14页珍藏版)》请在金锄头文库上搜索。

1、Android 通知 Notification 全面剖析通知通知是您可以在应用的常规 UI 外部向用户显示的消息。当您告知系统发出通知时,它将先以图标的形式显示在通知区域中。用户可以打开抽屉式通知栏查看通知的详细信息。 通知区域和抽屉式通知栏均是由系统控制的区域,用户可以随时查看。图 1. 通知区域中的通知。图 2. 抽屉式通知栏中的通知。注:除非特别注明,否则本指南均引用版本 4 支持库中的 NotificationCompat.Builder 类。Android 3.0(API 级别 11)中已添加类 Notification.Builder。设计注意事项作为 Android 用户界面的一

2、个重要组成部分,通知具有自己的设计指导方针。Android 5.0(API 级别 21)中引入的 Material Design 变更尤为重要。创建通知您可以在 NotificationCompat.Builder 对象中为通知指定 UI 信息和操作。要创建通知,请调用 NotificationCompat.Builder.build(),它将返回包含您的具体规范的 Notification 对象。要发出通知,请通过调用 NotificationManager.notify() 将 Notification 对象传递给系统。必需的通知内容Notification 对象必须包含以下内容:小图标,

3、由 setSmallIcon() 设置 标题,由 setContentTitle() 设置 详细文本,由 setContentText() 设置可选通知内容和设置所有其他通知设置和内容都是可选的。如需了解有关它们的更多详情,请参阅 NotificationCompat.Builder 参考文档。通知操作尽管通知操作都是可选的,但是您至少应向通知添加一个操作。 操作允许用户直接从通知转到应用中的 Activity,他们可在其中查看一个或多个事件或执行进一步的操作。一个通知可以提供多个操作。您应该始终定义一个当用户点击通知时会触发的操作;通常,此操作会在应用中打开 Activity。 您也可以向通

4、知添加按钮来执行其他操作,例如,暂停闹铃或立即答复短信;此功能自 Android 4.1 起可用。如果使用其他操作按钮,则您还必须使这些按钮的功能在应用的 Activity 中可用在 Notification 内部,操作本身由 PendingIntent 定义,后者包含在应用中启动 Activity 的 Intent。要将 PendingIntent 与手势相关联,请调用 NotificationCompat.Builder 的适当方法。例如,如果您要在用户点击抽屉式通知栏中的通知文本时启动 Activity,则可通过调用 setContentIntent() 来添加 PendingInten

5、t。在用户点击通知时启动 Activity 是最常见的操作场景。此外,您还可以在用户清除通知时启动 Activity。在 Android 4.1 及更高版本中,您可以通过操作按钮启动 Activity。通知优先级您可以根据需要设置通知的优先级。优先级充当一个提示,提醒设备 UI 应该如何显示通知。 要设置通知的优先级,请调用 NotificationCompat.Builder.setPriority() 并传入一个 NotificationCompat 优先级常量。有五个优先级别,范围从 PRIORITY_MIN (-2) 到 PRIORITY_MAX (2);如果未设置,则优先级默认为 P

6、RIORITY_DEFAULT (0)。有关设置适当优先级别的信息,请参阅通知设计指南中的“正确设置和管理通知优先级” 。创建简单通知以下代码段说明了一个指定某项 Activity 在用户点击通知时打开的简单通知。 请注意,该代码将创建 TaskStackBuilder 对象并使用它来为操作创建 PendingIntent。启动 Activity 时保留导航部分对此模式做了更详尽的阐述:NotificationCompat.Builder mBuilder =new NotificationCompat.Builder(this).setSmallIcon(R.drawable.notific

7、ation_icon).setContentTitle(My notification).setContentText(Hello World!);/ Creates an explicit intent for an Activity in your appIntent resultIntent = new Intent(this, ResultActivity.class);/ The stack builder object will contain an artificial back stack for the/ started Activity./ This ensures tha

8、t navigating backward from the Activity leads out of/ your application to the Home screen.TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);/ Adds the back stack for the Intent (but not the Intent itself)stackBuilder.addParentStack(ResultActivity.class);/ Adds the Intent that starts the

9、Activity to the top of the stackstackBuilder.addNextIntent(resultIntent);PendingIntent resultPendingIntent =stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);mBuilder.setContentIntent(resultPendingIntent);NotificationManager mNotificationManager =(NotificationManager) getSystemServi

10、ce(Context.NOTIFICATION_SERVICE);/ mId allows you to update the notification later on.mNotificationManager.notify(mId, mBuilder.build();就这么简单。您的用户现已收到通知。将扩展布局应用于通知要使通知出现在展开视图中,请先创建一个带有所需普通视图选项的 NotificationCompat.Builder 对象。接下来,调用以扩展布局对象作为其参数的 Builder.setStyle()。请记住,扩展通知在 Android 4.1 之前的平台上不可用。要了解如何

11、处理针对 Android 4.1 及更早版本平台的通知,请阅读处理兼容性部分。例如,以下代码段演示了如何更改在前面的代码段中创建的通知,以便使用扩展布局:NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this).setSmallIcon(R.drawable.notification_icon).setContentTitle(Event tracker).setContentText(Events received)NotificationCompat.InboxStyle inboxStyle =n

12、ew NotificationCompat.InboxStyle();String events = new String6;/ Sets a title for the Inbox in expanded layoutinboxStyle.setBigContentTitle(Event tracker details:);./ Moves events into the expanded layoutfor (int i=0; i ,其中, 是父 元素的 android:name 值。请参阅下面的 XML 示例。同样添加对 Android 4.1 及更高版本的支持。为此,请将 androi

13、d:parentActivityName 属性添加到正在启动的 Activity 的 activity 元素中。 最终的 XML 应如下所示:根据可启动 Activity 的 Intent 创建返回栈:创建 Intent 以启动 Activity。通过调用 TaskStackBuilder.create() 创建堆栈生成器。 通过调用 addParentStack() 将返回栈添加到堆栈生成器。 对于在清单文件中所定义层次结构内的每个 Activity,返回栈均包含可启动 Activity 的 Intent 对象。此方法还会添加一些可在全新任务中启动堆栈的标志。 注:尽管 addParentS

14、tack() 的参数是对已启动 Activity 的引用,但是方法调用不会添加可启动 Activity 的 Intent,而是留待下一步进行处理。通过调用 addNextIntent(),添加可从通知中启动 Activity 的 Intent。 将在第一步中创建的 Intent 作为 addNextIntent() 的参数传递。 如需,请通过调用 TaskStackBuilder.editIntentAt() 向堆栈中的 Intent 对象添加参数。有时,需要确保目标 Activity 在用户使用“返回”导航回它时会显示有意义的数据。 通过调用 getPendingIntent() 获得此返回

15、栈的 PendingIntent。 然后,您可以使用此 PendingIntent 作为 setContentIntent() 的参数。 以下代码段演示了该流程:.Intent resultIntent = new Intent(this, ResultActivity.class);TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);/ Adds the back stackstackBuilder.addParentStack(ResultActivity.class);/ Adds the Intent to the

16、 top of the stackstackBuilder.addNextIntent(resultIntent);/ Gets a PendingIntent containing the entire back stackPendingIntent resultPendingIntent =stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);.NotificationCompat.Builder builder = new NotificationCompat.Builder(this);builder.setContentIntent(resultPendingIntent);NotificationManager mNotificationManager =(NotificationManager) getSystemService(Context.NOTIFIC

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 办公文档 > 其它办公文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号