android 源码分析notification,# Notification 源码分析

引言

bfa3096bbdbb

notification.jpg

Notification 在v7版本下从4.0后增加了Media Style. 今天我们分析下Notification在v7版本的源码。有助于我们针对不同版本的Notification做出合适样式选择。

Notification使用流程

现在我们使用Notification基本都是如下步骤:

NotificationCompat.Builder builder = new NotificationCompat.Builder(context);

builder.setContentText

.setSmallIcon

.set...

Notification notification = builder.build();

notificationManager.notify(TYPE_BigText,notification);

我们看一下涉及到的类定义:

NotificationCompat

Helper for accessing features in Notification introduced after API level 4 in a backwards compatible fashion.

帮助Notification存储细节。

NotificationCompat.Builder

Builder class for NotificationCompat objects. Allows easier control over all the flags, as well as help constructing the typical notification layouts.

NotificationCompat对象的构造器类,构造Notification的样式。

当Notification构建好之后最后一个步骤:

NotificationManager

Post a notification to be shown in the status bar. If a notification with the same id has already been posted by your application and has not yet been canceled, it will be replaced by the updated information.

推送一个Notification到状态栏,如果应用中已经有了一个同样的Notification Id 将会被代替并更新。

Android support v7中的Notification相关类

v7对v4中的Notification进行扩展。

v7下NotificationCompat的定义

bfa3096bbdbb

v7_NotificationCompat.jpg

然后我来看下NotificationCompat.Builder的定义

bfa3096bbdbb

v7_NotificationCompat.Builder.jpg

Builder重写了v4下的getExtender() method,看下返回类型BuilderExtender 找到这样的定义

Interface for appcompat to extend v4 builder with media style.

appcompat 用来扩增v4 media style 的接口

我们再仔细看getExtender中的代码,针对不同版本sdk,返回不同版本的BuilderExtender.

针对ICE_CREAM_SANDWICH版本的BuilderExtender的代码看下:

bfa3096bbdbb

v7_Notification_Extend.jpg

内部调用add方法进行整理样式

用table显示不同版本扩展mediastyle的method.

版本 | 整理样式的方法

------------- | -------------| -------------

LOLLIPOP (>=Android 5.1) | addMediaStyleToBuilderLollipop

JELLY_BEAN (>=Android4.1&&

ICE_CREAM_SANDWICH (>=Android4.0&&

从这个表格可以知道 4.0以后才出现的Media Style.

再看v4下Builder.build method

bfa3096bbdbb

v4_NotificationCompat_Builder_build.jpg

最终是通过BuilderExtender构造Notification.

总结

Builder构造好样式后,会根据不同版本生成不同的BuilderExtender.

如果你想知道Notification在不同版本上的限制,你只需要查看不同版本的BuilderExtender的区别即可。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值