編輯:關於 Android 開發
想像一下,如果沒有 Notification,我們的 QQ、微信與其他應用就無法主動通知我們,我們得時時盯著手機檢查有沒有新訊息,那著實令人煩心。可見 Notification 的重要性。本篇會介紹三種通知形式:普通通知、折疊式通知與懸掛式通知,並補充 Android 5.0 的通知顯示等級設定。
首先建立 Builder 物件,用 PendingIntent 控制跳轉,這裡示範跳轉到網頁:
Notification.Builder builder = new Notification.Builder(this); Intent mIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://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());
效果如下圖:

折疊式 Notification 是一種自訂視圖的通知,適合顯示長文本或自訂版面。它有兩種狀態:普通狀態下的視圖(若不自訂,樣式與普通通知相同)與展開狀態下的視圖。由於顯示視圖的行程與建立視圖的行程不同,因此我們需要使用 RemoteViews。首先用它建立自訂視圖:
//用RemoteViews來創建自定義Notification視圖 RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.view_fold);
視圖的版面檔:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://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_marginLeft="150dp" android:layout_marginTop="30dp" android:text="展開後的自定義視圖" android:textColor="@color/colorPrimaryDark" /> </LinearLayout>
我們需要把自訂視圖指定給 Notification 的視圖。以下程式碼是把自訂視圖指定為展開時的視圖:
//指定展開時的視圖 notification.bigContentView = remoteViews;
當然,也可以指定為普通狀態時的視圖:
//指定普通狀態時的視圖 notification.contentView = remoteViews;
其餘程式碼與普通通知大同小異,折疊式的完整程式碼如下:
Notification.Builder builder = new Notification.Builder(this); Intent mIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://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 是 Android 5.0 新增的方式。與前兩種需要下拉通知欄才能看到不同,懸掛式通知會直接懸掛在螢幕上方,而且焦點仍停留在使用者正在操作的介面,不會打斷操作,過幾秒後自動消失。它需要呼叫 setFullScreenIntent 來實現:
//如果描述的PendingIntent已經存在,則在產生新的Intent之前會先取消掉當前的 PendingIntent hangPendingIntent = PendingIntent.getActivity(this, 0, hangIntent, PendingIntent.FLAG_CANCEL_CURRENT); builder.setFullScreenIntent(hangPendingIntent, true);
懸掛式通知的完整程式碼:
Notification.Builder builder = new Notification.Builder(this); Intent mIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://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());
效果如下:

Android 5.0 加入了通知的顯示等級,共有三種:
VISIBILITY_PUBLIC:任何情況都會顯示通知
VISIBILITY_PRIVATE:只有在沒有鎖屏時會顯示通知
VISIBILITY_SECRET:在安全鎖與沒有鎖屏的情況下顯示通知
設定非常簡單,只要呼叫 setVisibility 即可:
builder.setVisibility(Notification.VISIBILITY_PUBLIC);
我在這裡寫了一個方法來切換通知等級,用 RadioGroup 演示各個顯示等級,詳情請參照隨附的程式碼:
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; } }
Linux 2.6 內核協議棧系列:TCP 協議之數據發送
深入 Linux 2.6 內核 TCP 數據發送機制 在探討 TCP 發送函數之前,必須先認識一個關鍵結構 sk_buff。在 Linux 內核中,sk_buff 結構代
Intent(三):向下一個 Activity 傳遞資料
Intent(三):向下一個 Activity 傳遞資料 要往下傳遞資料到下一個 Activity 其實很簡單,可以採用 putExtra() 方法的重載版本,把想傳遞的
基於 MVVM 的知乎日報 Android 源碼解析
此專案以 MVVM 為核心,結合 Data Binding、Dagger 2、Retrofit 2 與 RxJava,展示現代 Android 應用常見的分層方式。技術重
AndroidAnnotations 框架的配置詳解
AndroidAnnotations 框架的配置詳解 一、在 Android Studio 下配置 AndroidAnnotations 首先準備好你的專案。Androi