Notification之 - Android5.0实现原理(一)

概述

写这篇文章的原由是当我自定义通知栏样式时,设置的view高度最终只能显示为通知栏的默认高度(64dp),让人十分困惑,因而研究了下源码。
除了解开真相意外,还了解到了包括bigview,pengdingintent的点击事件,通知栏限制等知识点,收获颇多,在这里分享给你们。但愿能让你们明白一个通知是如何添加到状态栏上面去的,若是遇到奇怪的问题不至于抓瞎。javascript

前提

本文是以android5.0的源码为基础的而且假设你们都知道notification的基本用法。java

构造通知

在咱们使用NotificationCompat.Builder对象设置完各类参数(小/大图标,标题,内容等)后,最后会调用build方法来获得一个Notification,而后使用NotificationManager来发出通知。咱们就先来看看build方法作了什么事。
NotificationCompat是v4包中的一个类,作了android各个版本的兼容,可是不管是哪一个版本,最后build方法都是调用的Notification的build方法,因此咱们直接看Notification的build方法。
frameworks/base/core/java/android/app/Notification.javaandroid

/** * Combine all of the options that have been set and return a new {@link Notification} * object. */
public Notification build() {
    ...    
    //设置通知的默认信息
    Notification n = buildUnstyled();

   //设置通知的样式信息
    if (mStyle != null) {
        n = mStyle.buildStyled(n);
    }
    ...
    return n;
}复制代码

方法的注释说得很清楚了,就是将全部的设置选项联合在一块儿返回一个新的通知。app

buildUnstyled

其中buildUnstyled()所说的默认信息就是在android4.0之前还不能展开的时候所包括的全部信息,包括大/小图标,时间,标题,内容,自定义view等信息。 ide

/** * Apply the unstyled operations and return a new {@link Notification} object. * @hide */
public Notification buildUnstyled() {
    Notification n = new Notification();
    n.when = mWhen;
    n.icon = mSmallIcon;
    ...
    setBuilderContentView(n, makeContentView());
    n.contentIntent = mContentIntent;
    ...
    setBuilderBigContentView(n, makeBigContentView());
    setBuilderHeadsUpContentView(n, makeHeadsUpContentView());
    // Note: If you're adding new fields, also update restoreFromNotitification().
    return n;
}复制代码

setBuilderContentView用于设置通知栏的ContentView属性布局

private void setBuilderContentView(Notification n, RemoteViews contentView) {
    n.contentView = contentView;
    ...
}复制代码

makeContentView()是构造出所须要填充的viewpost

private RemoteViews makeContentView() {
    if (mContentView != null) {
        return mContentView;
    } else {
        return applyStandardTemplate(getBaseLayoutResource());
    }
}复制代码

若是你没有使用自定view,将会使用标准的模板样式ui

private int getBaseLayoutResource() {
     return R.layout.notification_template_material_base;
}复制代码

这里只讲了setBuilderContentView(n, makeContentView());方法, 后面的setBuilderBigContentView(n, makeBigContentView());setBuilderHeadsUpContentView(n, makeHeadsUpContentView());方法与其相似都是设置通知的相应属性,直接给出结果,再也不累述spa

setBuilderHeadsUpContentView  -->  n.headsUpContentView
setBuilderBigContentView  --> n.bigContentView = bigContentView;

这样,通知的显示内容就已经构造好了。rest

1 .能够看出,在构造阶段,并无对通知栏的自定义view高度作出限制,但最后显示的时候倒是一个固定高度,why?
2.先记住这里的bigContentView属性,后面会在提到。

buildStyled

4.0后若是设置了BigText,BigPic等样式,则会调用buildStyled方法。buildStyled是Notification.Style中的一个方法

public Notification buildStyled(Notification wip) {
    ...
    populateBigContentView(wip);
    ...
    return wip;
}复制代码

populateBigContentView是一个protected所修饰的方法,具体的实现是在所设置的Style中,这里里BigPic Style为例,在BigPictureStyle类中,重写了该方法

@Override
public void populateBigContentView(Notification wip) {
    mBuilder.setBuilderBigContentView(wip, makeBigContentView());
}复制代码
private RemoteViews makeBigContentView() {
    RemoteViews contentView = getStandardView(mBuilder.getBigPictureLayoutResource());
    contentView.setImageViewBitmap(R.id.big_picture, mPicture);
    ...
    return contentView;
}复制代码
private int getBigPictureLayoutResource() {
    return R.layout.notification_template_material_big_picture;
}复制代码

若是是BigPic Style样式的通知,其实也是调用了系统中设置的一个模板布局notification_template_material_big_picture.
这里又调用了一次mBuilder.setBuilderBigContentView,前面提到了该方法是给notification的bigContentView的属性赋值。因此若是设置了样式,则会覆盖默认的bigContentView值

总结

在通知构造环节,咱们须要记住作了最重要的2件事

  1. 给Notification的contentView属性赋值,该值能够是自定义的view也能够是系统默认样式的view
  2. 给Notification的bigContentView属性赋值,该值可使自定义的view也能够是系统默认样式的view

相关阅读

Notification之----Android5.0实现原理(二)
Notification之----自定义样式
Notification之----默认样式
Notification之----任务栈

相关文章
相关标签/搜索