Android中的通知Notification介绍和自定义Notification
2012-10-16 13:43
453 查看
1.Notification介绍
Notification通过NotificationManager可以往状态栏中发送通知。在API Level 11之前构造一个Notification如下:
大致过程:
1.实例化一个Notification对象notify
2.给notify赋值,从icon到notify.setLatestEventInfo()
3.用NotificationMnager将notify发送出去
下面来看下setLatestEventInfo()这个被废弃的方法。
从源码中可以看出,setLatestEventInfo()这个方法内部会实例化一个RemoteViews对象再将
通知标题contentTitle和通知内容contentText用setTextVewText赋值给RemoteViews对象,在最后
this.contentView = contentView再将RemoteViews对象赋值给notification中字段contentView.
是不是这边有一种绕来绕去的感觉,是不是有点不干脆?
因为Notification中没有直接能设置通知标题和通知内容的方法,通知的标题和内容是在RemoteViews,因此需要先给RemoteViews
对象赋值再将RemoteViews对象给Notification.
在API Level 11之后,Notification引入了一个内部类Builder,将原来这种方法改变了。现在这个Builder提供构造一个Notification的所有的要素,所以setLatestEventInfo()就被废弃了。这样设计更加合理也更加简单容易理解。
当然以前的方法还是可以用的,就是直接给Notification对象的字段赋值。
在新的版本中废弃的方法最好还是不要用了。
2.自定义Notification
下面来一段API中的介绍:By default, the notification that appears in the notifications window includes a title and the message text. These are defined by thecontentTitle and
contentText parameters of the
setLatestEventInfo()
method. However, you can also define a custom layout for the notification using
RemoteViews.
To define your own layout for the notification, instantiate a
RemoteViewsobject that inflates a custom layout file, then
pass the
RemoteViewsto thecontentView field of your Notification.
一般来说,出现在通知窗口中的通知都包含标题和内容。这些都是在setLatestEventInfo()中的两个参数thecontentTitle
and contentText所定义的。然而你也可以用RemoteViews给Notification进行自定义的布局。
要定义自己的Notification布局,要实例化一个RemoteViews对象,该对象需要引入一个自定义的布局文件,然后将该RemoteViews对象赋值给Notification中的contentView字段。
或者用builder来设置也可以。
builder.setContent(views);
因为这个R.layout.self_layout是自己在xml中定义的,所以需要什么样式都可以了。
相关文章推荐
- Android中的通知Notification介绍和自定义Notification
- Android自定义通知布局Notification,点击Notification导航切换回原Activity
- Android中通知栏Notification详解以及自定义Notification
- Android Notification自定义通知样式你要知道的事
- 59 Android Notification 自定义通知
- android自定义Notification通知栏实例
- android Notification的自定义和实现通知栏的展开和收起
- Android Notification自定义通知样式你要知道的事
- android中notification通知及自定义notification
- Android 自定义Notification通知样式并设置按钮监听.
- Android通知栏实现APP下载更新(DownloadManager和自定义Notification2种方式实现)
- 使用 Android 自定义通知Notification时遇到的问题
- Android Notification自定义通知样式你要知道的事
- Android 通知栏Notification,NotificationManager,RemoteViews使用流程,自定义通知栏样式简介
- Android自定义通知布局Notification,点击Notification导航切换回原Activity
- Android种使用Notification实现通知管理以及自定义通知栏实例(示例四)
- Android 通知栏发送消息Notification(可以使用自定义的布局)
- android 自定义通知布局Notification,点击Notification导航切换回原Activity
- Android自定义通知布局Notification,点击Notification导航切换回原Activity
- Android Notification自定义通知样式你要知道的事