編輯:關於 Android 開發
系統預設的通知樣式只有小圖示、標題與內容三行字,想要放圖片、控制字級顏色、甚至讓某個區塊可點擊,就得動用 RemoteViews。本文示範如何把自訂版面配置塞進通知欄。
通知是顯示在系統行程(System UI)當中的,而你的 App 跑在自己的行程裡。兩者之間不能直接傳遞 View 物件,因此 Android 提供 RemoteViews 作為中介——它攜帶的是「版面配置 ID + 一連串對元件的操作指令」,系統行程收到後自行 inflate 並套用。理解這點,就能明白為什麼 RemoteViews 只支援有限的幾種操作(設文字、設圖片、設點擊),而不能任意操作。
package com.example.mvp;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.graphics.Color;
import android.os.Bundle;
import android.content.Context;
import android.content.Intent;
import android.widget.RemoteViews;
public class MainActivity extends Activity implements ILoginView {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Notification notification = new Notification();
// 設定圖示:後續自訂版面的圖片會覆蓋它,但仍必須設定,否則通知不會出現在通知欄
notification.icon = R.drawable.ic_launcher;
notification.tickerText = "hello I am gougou";
notification.when = System.currentTimeMillis();
notification.flags = Notification.FLAG_AUTO_CANCEL;
// 傳入目前專案的套件名稱,以及通知欄上要顯示的自訂版面 ID
RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.activity_amain);
// 以下都是設定通知欄版面中控制項的屬性
remoteViews.setImageViewResource(R.id.imageView1, R.drawable.hot);
remoteViews.setTextColor(R.id.textView1, Color.RED);
remoteViews.setTextViewText(R.id.textView1, "hello I am 小籃子");
remoteViews.setTextViewTextSize(R.id.textView1, 1, 15);
// PendingIntent 有四種 flag:
// FLAG_ONE_SHOT 只執行一次
// FLAG_NO_CREATE 若描述的 Intent 不存在則回傳 NULL
// FLAG_CANCEL_CURRENT 若已存在則先取消再產生新的
// FLAG_UPDATE_CURRENT 總是執行更新,最常使用
PendingIntent pendingIntent = PendingIntent.getActivity(
this, 0, new Intent(this, AMainActivity.class),
PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.textView1, pendingIntent);
notification.contentView = remoteViews;
notification.contentIntent = pendingIntent;
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(1, notification);
}
}
這份配置就是實際顯示在通知列上的樣子,用權重把三個元件等分:
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".AMainActivity" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
<TextView
android:id="@+id/textView1"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:clickable="true"
/>
<TextView
android:id="@+id/textView2"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
/>
</LinearLayout>
icon 一定要設。即使自訂版面裡已經有 ImageView,notification.icon 仍是必填。系統在狀態列上顯示的小圖示取自這個欄位,漏設會導致通知根本不會出現。
RemoteViews 的操作有限。它只支援一組預先定義的方法,例如 setTextViewText、setImageViewResource、setTextColor、setProgressBar 等。想放自訂 View 或做複雜動畫是行不通的。
PendingIntent 的 requestCode 與 flag 有學問。若有多個通知要各自導向不同頁面,requestCode 必須區分開來,否則會因為 Intent 比對相同而被系統視為同一個,導致點了都跳到同一處。
這套 API 已屬舊世代。上述 new Notification() 搭配 contentView 的寫法,在 Android 5.0 起被標記為 deprecated,Android 8.0(API 26)之後更強制要求建立 NotificationChannel,否則通知完全不會顯示。新專案請改用 NotificationCompat.Builder,並視需要搭配 DecoratedCustomViewStyle 或 RemoteViews 的 setCustomContentView。本文的寫法適合作為理解 RemoteViews 機制的入門教材。
Android 四大組件之 Broadcast 廣播詳解
廣播的概念 現實中,我們常通過電台發送廣播發布消息,買個收音機就能收聽。在 Android 中也是類似:系統在產生某個事件時發送廣播,應用通過廣播接收者接收這個廣播,就能
使用 getDrawable 時遇到 NoSuchMethodError 的解法
使用 getDrawable 時遇到 NoSuchMethodError 的解法 在為 TextView 設定圖示或背景時,若直接呼叫某些 Resources 或 Con
模擬微信聊天菜單:OptionsMenu 與 ContextMenu 的配合使用
在微信裡長按一條聊天記錄,會從底部彈出「標記為未讀 / 置頂聊天 / 刪除該聊天」的浮層。這個交互在原生 Android 中,可以用 OptionsMenu 與 Cont
Android Activity 生命週期完整解析(含 onXxx 回呼與三種狀態)
Android Activity 生命週期完整解析(含 onXxx 回呼與三種狀態) 新建一個專案,MainActivity 的程式碼如下,我們藉由它來觀察各個生命週期方