Android5.x Notification應用解析

Notification可以讓我們在獲得消息的時候,在狀態欄,鎖屏界面來顯示相應的信息,很難想象如果沒有Notification,那我們的qq和微信以及其他應用沒法主動通知我們,我們就需要時時的看手機來檢查是否有新的信息和提醒著實讓人煩心,也體現出Notification重要性。這里會介紹三種Notification,分別是普通的Notification,折疊式Notification和懸掛式Notification。

1. 普通Notification

首先創建Builder 對象,用PendingIntent 控制跳轉,這里跳轉到網頁

Notification.Builder builder = new Notification.Builder(this);
Intent mIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://blog.csdn.net/itachi85/"));
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, mIntent, 0);

有了builder 我們就可以給Notification添加各種屬性:

 builder.setContentIntent(pendingIntent);
 builder.setSmallIcon(R.drawable.lanucher);
 builder.setLargeIcon(BitmapFactory.decodeResource(getResources(),     R.drawable.lanucher));
 builder.setAutoCancel(true);
 builder.setContentTitle("普通通知");

最后是創建NotificationManager調用notify方法:

  notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
  notificationManager.notify(0, builder.build());

來看看效果:


2. 折疊式Notification
折疊式Notification是一種自定義視圖的Notification,用來顯示長文本和一些自定義的布局的場景。它有兩種狀態,一種是普通狀態下的視圖(如果不是自定義的話和上面普通通知的視圖樣式一樣),一種是展開狀態下的視圖。和普通Notification不同的是,我們需要自定義的視圖,而這個視圖顯示的進程和我們創建視圖的進程不再一個進程,所以我們需要使用RemoteViews,首先要使用RemoteViews來創建我們的自定義視圖:

 //用RemoteViews來創建自定義Notification視圖
RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.view_fold);

視圖的布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:background="@drawable/fold"
    android:orientation="horizontal">
<ImageView
    android:id="@+id/iv_image"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:src="@drawable/fold"
    />

    <TextView
        android:id="@+id/tv_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:layout_marginLeft="150dp"
        android:text="展開后的自定義視圖"
        android:textColor="@color/colorPrimaryDark"/>
</LinearLayout>

我們需要把自定義的視圖賦值給Notification的視圖,下面代碼是把自定義視圖賦值給Notification展開時的視圖

//指定展開時的視圖
notification.bigContentView = remoteViews;

當然我們也可以把自定義視圖賦值給Notification普通狀態時的視圖

//指定普通狀態時的視圖
notification.contentView = remoteViews;

其他的代碼和普通Notification沒什么區別,折疊式Notification完整代碼:

        Notification.Builder builder = new Notification.Builder(this);
        Intent mIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://blog.csdn.net/itachi85/"));
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, mIntent, 0);
        builder.setContentIntent(pendingIntent);
        builder.setSmallIcon(R.drawable.foldleft);
        builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.lanucher));
        builder.setAutoCancel(true);
        builder.setContentTitle("折疊式通知");
        //用RemoteViews來創建自定義Notification視圖
        RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.view_fold);
        Notification notification = builder.build();
        //指定展開時的視圖
        notification.bigContentView = remoteViews;
        notificationManager.notify(1, notification);

如果不是自定義普通狀態視圖的話,折疊式Notification普通狀態下和普通Notification沒什么區別


我們接著往下拉,使折疊式Notification完全展開就會出現我們自定義的視圖

3. 懸掛式Notification
懸掛式Notification是android5.0新增加的方式,和前兩種顯示方式不同的是,前兩種需要下拉通知欄才能看到通知,而 懸掛式Notification不需要下拉通知欄就直接顯示出來懸掛在屏幕上方并且焦點不變仍在用戶操作的界面因此不會打斷用戶的操作,過幾秒就會自動消失。
和前兩種Notification不同的是,他需要調用setFullScreenIntent來將Notification變為懸掛式Notification

 //如果描述的PendingIntent已經存在,則在產生新的Intent之前會先取消掉當前的
        PendingIntent hangPendingIntent = PendingIntent.getActivity(this, 0, hangIntent, PendingIntent.FLAG_CANCEL_CURRENT);
        builder.setFullScreenIntent(hangPendingIntent, true);

實現懸掛式Notification完整代碼:

        Notification.Builder builder = new Notification.Builder(this);
        Intent mIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://blog.csdn.net/itachi85/"));
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, mIntent, 0);
        builder.setContentIntent(pendingIntent);
        builder.setSmallIcon(R.drawable.foldleft);
        builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.lanucher));
        builder.setAutoCancel(true);
        builder.setContentTitle("懸掛式通知");
        //設置點擊跳轉
        Intent hangIntent = new Intent();
        hangIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        hangIntent.setClass(this, MyNotificationActivity.class);
        //如果描述的PendingIntent已經存在,則在產生新的Intent之前會先取消掉當前的
        PendingIntent hangPendingIntent = PendingIntent.getActivity(this, 0, hangIntent, PendingIntent.FLAG_CANCEL_CURRENT);
        builder.setFullScreenIntent(hangPendingIntent, true);
        notificationManager.notify(2, builder.build());

來看看效果


4. Notification的顯示等級
android5.0加入了一種新的模式Notification的顯示等級,共有三種:

  • VISIBILITY_PUBLIC 只有在沒有鎖屏時會顯示通知
  • VISIBILITY_PRIVATE 任何情況都會顯示通知
  • VISIBILITY_SECRET 在安全鎖和沒有鎖屏的情況下顯示通知

設置非常簡單只要調用setVisibility方法就可以了

  builder.setVisibility(Notification.VISIBILITY_PUBLIC);

我在這里寫了個方法來設置Notification等級,用radioGroup來演示Notification的各個顯示等級,詳情請參照源碼。

  private void selectNotofovatiomLevel(Notification.Builder builder) {
        switch (radioGroup.getCheckedRadioButtonId()) {
            case R.id.rb_public:
                builder.setVisibility(Notification.VISIBILITY_PUBLIC);
                builder.setContentText("public");
                break;
            case R.id.rb_private:
                builder.setVisibility(Notification.VISIBILITY_PRIVATE);
                builder.setContentText("private");
                break;
            case R.id.rb_secret:
                builder.setVisibility(Notification.VISIBILITY_SECRET);
                builder.setContentText("secret");
                break;
            default:
                builder.setVisibility(Notification.VISIBILITY_PUBLIC);
                builder.setContentText("public");
                break;

        }
    }

源碼下載

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 228,646評論 6 533
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 98,595評論 3 418
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 176,560評論 0 376
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,035評論 1 314
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,814評論 6 410
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 55,224評論 1 324
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,301評論 3 442
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,444評論 0 288
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 48,988評論 1 335
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 40,804評論 3 355
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 42,998評論 1 370
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,544評論 5 360
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,237評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,665評論 0 26
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,927評論 1 287
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,706評論 3 393
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 47,993評論 2 374

推薦閱讀更多精彩內容

  • 原文出處: http://www.androidchina.net/6174.html Notification在...
    木木00閱讀 12,360評論 3 32
  • 內容抽屜菜單ListViewWebViewSwitchButton按鈕點贊按鈕進度條TabLayout圖標下拉刷新...
    皇小弟閱讀 46,851評論 22 665
  • 發現 關注 消息 iOS 第三方庫、插件、知名博客總結 作者大灰狼的小綿羊哥哥關注 2017.06.26 09:4...
    肇東周閱讀 12,153評論 4 61
  • 你的眼眸蘊化星辰萬千,點綴了我的夜空;一個微笑、一個擁抱,也勝過了全世界給我的祝福。而你總是那么羞澀,不愿在我的夢...
    玖寒閱讀 1,237評論 0 1
  • “有沒有覺得外面很吵?”阿枚推了推旁邊的丈夫。 明興睡意昏沉,但稍微側耳傾聽兩秒,這個時候已經凌晨一點多了吧,外面...
    張天狗閱讀 799評論 2 9