寫這篇文章的原由是當我自定義通知欄樣式時,設置的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()所說的默認信息就是在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屬性,後面會在提到。
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件事
Notification之----Android5.0實現原理(二)
Notification之----自定義樣式
Notification之----默認樣式
Notification之----任務棧