日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發(fā)布時間:2024/7/23 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android 源码分析notification,# Notification 源码分析 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

引言

notification.jpg

Notification 在v7版本下從4.0后增加了Media Style. 今天我們分析下Notification在v7版本的源碼。有助于我們針對不同版本的Notification做出合適樣式選擇。

Notification使用流程

現(xiàn)在我們使用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存儲細節(jié)。

NotificationCompat.Builder

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

NotificationCompat對象的構(gòu)造器類,構(gòu)造Notification的樣式。

當Notification構(gòu)建好之后最后一個步驟:

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到狀態(tài)欄,如果應用中已經(jīng)有了一個同樣的Notification Id 將會被代替并更新。

Android support v7中的Notification相關類

v7對v4中的Notification進行擴展。

v7下NotificationCompat的定義

v7_NotificationCompat.jpg

然后我來看下NotificationCompat.Builder的定義

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的代碼看下:

v7_Notification_Extend.jpg

內(nèi)部調(diào)用add方法進行整理樣式

用table顯示不同版本擴展mediastyle的method.

版本 | 整理樣式的方法

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

LOLLIPOP (>=Android 5.1) | addMediaStyleToBuilderLollipop

JELLY_BEAN (>=Android4.1&&

ICE_CREAM_SANDWICH (>=Android4.0&&

從這個表格可以知道 4.0以后才出現(xiàn)的Media Style.

再看v4下Builder.build method

v4_NotificationCompat_Builder_build.jpg

最終是通過BuilderExtender構(gòu)造Notification.

總結(jié)

Builder構(gòu)造好樣式后,會根據(jù)不同版本生成不同的BuilderExtender.

如果你想知道Notification在不同版本上的限制,你只需要查看不同版本的BuilderExtender的區(qū)別即可。

總結(jié)

以上是生活随笔為你收集整理的android 源码分析notification,# Notification 源码分析的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。