Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android5.x Notification應用解析

Android5.x Notification應用解析

編輯:關於Android編程

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<喎?/kf/ware/vc/" target="_blank" class="keylink">vc3Ryb25nPjxiciAvPg0K1du1/sq9Tm90aWZpY2F0aW9uysfSu9bW19S2qNLlytPNvLXETm90aWZpY2F0aW9uo6zTw8C0z9TKvrOkzsSxvrrN0rvQqdfUtqjS5bXEsry+1rXEs6G+sKGjy/zT0MG91tbXtMyso6zSu9bWysfG1c2o17TMrM/CtcTK0828o6jI57n7srvKx9fUtqjS5bXEu7C6zcnPw+bG1c2ozajWqrXEytPNvNH5yr3Su9H5o6mjrNK71tbKx9W5v6rXtMysz8K1xMrTzbyho7rNxtXNqE5vdGlmaWNhdGlvbrK7zay1xMrHo6zO0sPH0OjSqtfUtqjS5bXEytPNvKOstvjV4rj2ytPNvM/Uyr61xL34s8y6zc7Sw8e0tL2oytPNvLXEvfizzLK71NnSu7j2vfizzKOsy/nS1M7Sw8fQ6NKqyrnTw1JlbW90ZVZpZXdzo6zK18/I0qrKudPDUmVtb3RlVmlld3PAtLS0vajO0sPHtcTX1Lao0uXK0828OjwvcD4NCjxwcmUgY2xhc3M9"brush:java;"> //用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;

        }
    }

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved