編輯:關於 Android 程式設計
很難想像沒有 Notification 的手機生活:QQ、微信、各種 App 都靠它主動提醒我們,否則每分鐘都得自己解鎖檢查有沒有新訊息。以下介紹三種通知形式,以及 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.setContentIntent(pendingIntent);
builder.setSmallIcon(R.drawable.lanucher);
builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.lanucher));
builder.setAutoCancel(true); // 點擊後自動消失
builder.setContentTitle("普通通知");
最後透過 NotificationManager 發出:
notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, builder.build());

折疊式通知的重點是「收起時一個樣子、展開後另一個樣子」,因此要用 RemoteViews 自訂視圖。
// 建立自訂通知視圖
RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.view_fold);
對應的版面檔:
<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="展開後的自訂視圖" />
</LinearLayout>
接著決定這個自訂視圖要套在哪個狀態:
// 指定展開時的視圖
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 remoteViews = new RemoteViews(getPackageName(), R.layout.view_fold);
Notification notification = builder.build();
notification.bigContentView = remoteViews; // 展開時顯示
notificationManager.notify(1, notification);
如果沒有自訂收起狀態的視圖,折疊式通知在收起時看起來就跟普通通知沒兩樣:

往下拉到底展開後,才會看到自訂的版面:

懸掛式(Heads-up)是 Android 5.0 新增的顯示方式。與前兩者的差別在於:它不需要下拉通知欄,會直接浮在螢幕上方,而且不改變目前焦點,使用者原本的操作不會被打斷,幾秒後自動消失。
關鍵是改用 setFullScreenIntent():
// 若描述相符的 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);
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:安全鎖定時完全不顯示通知。設定方式很簡單:
builder.setVisibility(Notification.VISIBILITY_PUBLIC);
用 RadioGroup 做一個切換示範:
private void selectNotificationLevel(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;
}
}
本文以 Notification.Builder 為例,這是 5.x 時代的寫法。從 Android 8.0(API 26)開始,所有通知都必須綁定通知渠道(NotificationChannel),否則通知根本不會顯示;同時也必須動態申請 POST_NOTIFICATIONS 權限(API 33+)。目前建議改用 NotificationCompat.Builder 搭配 AndroidX,才能同時兼顧新版 API 與向下相容。
栈的应用:如何用两个队列实现一个栈
用两个队列实现一个栈 之前我实现过“两个栈实现一个队列”这道面试题,思路其实很简单,就是充分利用栈后进先出的特性:把输入的数据先压入栈 1,再把栈 1 的数据倒到栈 2,
Android Camera HAL3 Capture 模式:模塊交互與幀資料回調解析
本文以 Android 5.1 的 Camera3 HAL 為基礎,整理拍照 Capture 模式下,Camera2Client、CaptureSequencer、Jpe
Android 下拉刷新實戰:SwipeRefreshLayout 用法與踩坑記錄
只要是會聯網拉取資料的 App,幾乎都離不開下拉刷新這個互動。過去要實現它,通常得自己繼承 ListView 或 RecyclerView,重寫 onTouch、onSc
Android 監聽 ScrollView 的滑動事件
開發時常需要監聽 ScrollView 的滑動情況,例如滑動了多少距離、是否滑到頂部或底部。可惜 SDK 並未直接提供對外的方法,雖然 ScrollView 內部有一個