Android文档笔记:通知(一)
2013-04-18 16:59
363 查看
通知的显示元素
普通视图
- 高度64dp
- 大试图的通知在展开前也显示为普通视图
1. 标题
2. 大图标
3. 内容文字
4. 内容信息
5. 小图标
6.
通知的时间。默认为系统发出通知的时间,也可通过setWhen()来设置
大视图
- 只在通知被展开时显示
- 何时展开:通知处在顶端,或者用户通过收拾展开
- 收件箱风格的通知:
相比普通视图,仅多出:
7. 详情区域
- 详情区域根据用途可有多种风格:
>
大图片风格:详情区域包含一个256dp高度的位图
>
大文字风格:显示一个大的文字块
>
收件箱风格:显示多行文字
- 各种风格都具有以下常规视图不具有的内容选项:
>
大标题:在展开视图时替代普通视图的骠骑
>
总结文字:允许你在详情区域之下增加一行内容
- 关于大视图,具体看本文章节:《对通知使用大视图风格》
创建一个通知
- 用NotificationCompat.Builder对象来设定通知的UI信息和行为
- 调用NotificationCompat.Builder.build()来创建通知
- 调用NotificationManager.notify()来发送通知
必要的通知内容
一条通知必须包含:
-
一个小图标,用setSmallIcon()来设置
-
一条标题,用setContentTitle()来设置
-
详情文字,用setContentText()来设置
可选的通知内容与设置
-
其余所有项目都是可选的,具体参阅NotificationCompat.Builder参考文档
通知的行为
-
虽然是可选项,但是你应当为你的通知添加至少一个行为。
- 行为允许用户直接转向应用中的一个活动。
- 通知可以提供多个行为:
>
你应当总是定义用户点击所触发的行为。这个行为通常是用来打开一个活动。
>
你还可以在通知中添加按钮以执行附加的动作,如延迟一个闹钟或者快速回复一条短信。这个特性从Android
4.1开始可用。
>
如果使用附加行为按钮,那么在你的活动中也必须使这些功能可用。
- 如何定义行为?
>
行为由一个PendingIntent来定义。PendingIntent包含一个用来启动活动的intent
>
为了将PendingIntent与手势相关联,应当调用NotificationCompat.Builder的对应方法。如,如果你想要在点击通知的内容时启动一个活动,则要调用setContentIntent()方法来添加PendingIntent。
创建简单通知
-
以下代码片段掩饰如何创建一个简单通知。为此通知指定了一个活动。当用户惦记通知时打开该活动。
-
代码中创建了一个TaskStackBuilder对象,以之来创建PendingIntent。具体在《在启动活动时保存导航》一节讲解
对通知使用大视图风格
- 首先创建NotificationCompat.Builder对象来做普通的设置
- 然后调用Builder.setStyle()
-
4.1之前的版本不支持大视图,参见《处理兼容性》一节
- 示例:
处理兼容性
-
为确保最佳兼容性,应当用NotificationCompat及其子类来创建通知,特别是NotificationCompt.Builder。
另外,应当根据以下步骤来实现一个通知:
-
将通知提供的所有功能提供给所有用户,无论他们使用的android是什么版本。要做到这一点,就应当保证这些功能也存在于你的应用的活动里面。不妨新增一个活动来达到这个目的。反过来说,任何功能应当首先在活动中实现。
-
确保所有用户都能通过惦记通知到达活动中功能。这需要为活动创建PendingIntent。调用setContentIntent()来为通知添加PendingIntent。
-
最后,添加可展开的内容。记住,加入的任何功能也必须在点击通知之后启动的活动中得以体现。
普通视图
- 高度64dp
- 大试图的通知在展开前也显示为普通视图
1. 标题
2. 大图标
3. 内容文字
4. 内容信息
5. 小图标
6.
通知的时间。默认为系统发出通知的时间,也可通过setWhen()来设置
大视图
- 只在通知被展开时显示
- 何时展开:通知处在顶端,或者用户通过收拾展开
- 收件箱风格的通知:
相比普通视图,仅多出:
7. 详情区域
- 详情区域根据用途可有多种风格:
>
大图片风格:详情区域包含一个256dp高度的位图
>
大文字风格:显示一个大的文字块
>
收件箱风格:显示多行文字
- 各种风格都具有以下常规视图不具有的内容选项:
>
大标题:在展开视图时替代普通视图的骠骑
>
总结文字:允许你在详情区域之下增加一行内容
- 关于大视图,具体看本文章节:《对通知使用大视图风格》
创建一个通知
- 用NotificationCompat.Builder对象来设定通知的UI信息和行为
- 调用NotificationCompat.Builder.build()来创建通知
- 调用NotificationManager.notify()来发送通知
必要的通知内容
一条通知必须包含:
-
一个小图标,用setSmallIcon()来设置
-
一条标题,用setContentTitle()来设置
-
详情文字,用setContentText()来设置
可选的通知内容与设置
-
其余所有项目都是可选的,具体参阅NotificationCompat.Builder参考文档
通知的行为
-
虽然是可选项,但是你应当为你的通知添加至少一个行为。
- 行为允许用户直接转向应用中的一个活动。
- 通知可以提供多个行为:
>
你应当总是定义用户点击所触发的行为。这个行为通常是用来打开一个活动。
>
你还可以在通知中添加按钮以执行附加的动作,如延迟一个闹钟或者快速回复一条短信。这个特性从Android
4.1开始可用。
>
如果使用附加行为按钮,那么在你的活动中也必须使这些功能可用。
- 如何定义行为?
>
行为由一个PendingIntent来定义。PendingIntent包含一个用来启动活动的intent
>
为了将PendingIntent与手势相关联,应当调用NotificationCompat.Builder的对应方法。如,如果你想要在点击通知的内容时启动一个活动,则要调用setContentIntent()方法来添加PendingIntent。
创建简单通知
-
以下代码片段掩饰如何创建一个简单通知。为此通知指定了一个活动。当用户惦记通知时打开该活动。
-
代码中创建了一个TaskStackBuilder对象,以之来创建PendingIntent。具体在《在启动活动时保存导航》一节讲解
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.notification_icon) .setContentTitle("My notification") .setContentText("Hello World!"); // Creates an explicit intent for an Activity in your app Intent resultIntent = new Intent(this, ResultActivity.class); // The stack builder object will contain an artificial back stack for the // started Activity. // This ensures that 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 Activity to the top of the stack stackBuilder.addNextIntent(resultIntent); PendingIntent resultPendingIntent = stackBuilder.getPendingIntent( 0, PendingIntent.FLAG_UPDATE_CURRENT ); mBuilder.setContentIntent(resultPendingIntent); NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); // mId allows you to update the notification later on. mNotificationManager.notify(mId, mBuilder.build());
对通知使用大视图风格
- 首先创建NotificationCompat.Builder对象来做普通的设置
- 然后调用Builder.setStyle()
-
4.1之前的版本不支持大视图,参见《处理兼容性》一节
- 示例:
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.notification_icon) .setContentTitle("Event tracker") .setContentText("Events received") NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle(); String[] events = new String[6]; // Sets a title for the Inbox style big view inboxStyle.setBigContentTitle("Event tracker details:"); ... // Moves events into the big view for (int i=0; i < events.length; i++) { inboxStyle.addLine(events[i]); } // Moves the big view style object into the notification object. mBuilder.setStyle(inBoxStyle); ... // Issue the notification here.
处理兼容性
-
为确保最佳兼容性,应当用NotificationCompat及其子类来创建通知,特别是NotificationCompt.Builder。
另外,应当根据以下步骤来实现一个通知:
-
将通知提供的所有功能提供给所有用户,无论他们使用的android是什么版本。要做到这一点,就应当保证这些功能也存在于你的应用的活动里面。不妨新增一个活动来达到这个目的。反过来说,任何功能应当首先在活动中实现。
-
确保所有用户都能通过惦记通知到达活动中功能。这需要为活动创建PendingIntent。调用setContentIntent()来为通知添加PendingIntent。
-
最后,添加可展开的内容。记住,加入的任何功能也必须在点击通知之后启动的活动中得以体现。
相关文章推荐
- Android文档笔记:通知(一)
- Android文档笔记:通知(二)
- Android文档笔记:通知(二)
- Android文档笔记:通知(三) - 启动活动时保存导航
- Android文档笔记:通知(三)&nbsp;-&nbsp;启…
- Android文档笔记:通知(四)&amp;nbsp…
- Android文档笔记:通知(四) - 在通知中显示进度
- Android 文档笔记
- Android文档笔记:服务(一)
- Android开发笔记(五十二)通知推送Notification
- 通知:Android 官方文档翻译正式开动了
- Android TableLayout官方文档 例子学习笔记
- Android官方开发文档Training系列课程中文版:通知用户之在通知中显示进度
- 安卓学习笔记---Android通知栏微技巧,通知栏图标在sdk21以上及以下的区别
- Android文档笔记:服务(二)-创建…
- [翻译]Android官方文档 - 通知(Notifications)
- Android文档笔记:动画与图形(一)
- [Android | Material Design] ToolBar 通知栏变色处理小问题 笔记
- android 文档学习笔记
- Android官方开发文档Training系列课程中文版:通知用户之更新或移除通知