Android5.x Notification應用解析

來源:互聯網
上載者:User

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);

視圖的布局檔案:

    

我們需要把自訂的視圖賦值給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;        }    }

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.