編輯:關於 Android 程式設計
通知(Notification)是 App 在「不搶走使用者焦點、不打斷當前操作」的前提下,向使用者傳遞訊息的機制。它幾乎總是與 NotificationManager 搭配使用:前者描述「長什麼樣子」,後者負責「把它推出去」。
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(ns);
Notification notification = new Notification(); notification.icon = R.drawable.notification_icon;
也可以一次帶入三個參數,語意更清楚:
int icon = R.drawable.notification_icon; // 通知圖示 CharSequence tickerText = "Hello"; // 狀態欄的提示文字 long when = System.currentTimeMillis(); // 通知產生時間 Notification notification = new Notification(icon, tickerText, when);
Context context = getApplicationContext(); CharSequence contentTitle = "My notification"; CharSequence contentText = "Hello World!"; Intent notificationIntent = new Intent(this, MyClass.class); PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
接著依需求加上提示音、震動與呼吸燈:
// 提示音:使用系統預設或自訂音效
notification.defaults |= Notification.DEFAULT_SOUND;
notification.sound = Uri.parse("file:///sdcard/notification/ringer.mp3");
// 震動:陣列第一個值是靜默時間,其後為「震動、靜默」交替
notification.defaults |= Notification.DEFAULT_VIBRATE;
notification.vibrate = new long[]{0, 100, 200, 300};
// LED 呼吸燈
notification.defaults |= Notification.DEFAULT_LIGHTS;
notification.ledARGB = 0xff00ff00;
notification.ledOnMS = 300;
notification.ledOffMS = 1000;
notification.flags |= Notification.FLAG_SHOW_LIGHTS;
private static final int ID_NOTIFICATION = 1; mNotificationManager.notify(ID_NOTIFICATION, notification);
若希望使用者手動清除前通知都不消失,加上 notification.flags = Notification.FLAG_NO_CLEAR; 即可。
更新非常直覺:重新設定好屬性後,用相同的 ID 再呼叫一次 notify()。無論你帶入的是同一個物件還是全新的物件,系統都會以 ID 為鍵去替換狀態列圖示與展開後的詳細內容。這個特性很適合做「下載進度 30% → 60% → 100%」的即時更新。
notificationManager.cancel(notificationRef);
取消後,狀態列圖示會被移除,展開的詳細資訊也會一併清除。共有四種取消途徑:呼叫 cancel(id)、呼叫 cancelAll()、使用者手動滑掉,或加上 Notification.FLAG_AUTO_CANCEL 讓點擊後自動消失。
上面這套以 setLatestEventInfo() 為核心的寫法,在 API 23 之後已被移除。Android 8.0(API 26)起更強制要求建立通知 channel,否則通知根本不會顯示。目前的標準寫法是改用 NotificationCompat.Builder:
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("My notification")
.setContentText("Hello World!")
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setAutoCancel(true)
.setContentIntent(contentIntent);
NotificationManagerCompat.from(this).notify(ID_NOTIFICATION, builder.build());
同時別忘了在 AndroidManifest.xml 宣告 android.permission.POST_NOTIFICATIONS,並在 Android 13 以上執行期向使用者詢問通知權限。
Android ListView 出現「The specified child already has a parent」的原因與解法
在 Android 的 ListView 開發中,若於 getView 裡把外部傳入的 Poker 控制項直接加入 convertView,上下滑動時很容易看到「The
Android 通過 Path 實現復雜效果(搜索按鈕 + 時鐘的實現)
Path:在 Android 中,絕大多數複雜圖形的繪制都是通過 Path 實現的,比如繪製一條曲線並讓物體沿其運動,比如搜索按鈕,比如一個簡單的時鐘。 定義:Path
Android 開發入門:UI 元件、Activity 與事件機制全解析
聲明 本文由 William Yi 整理,是獻給 AS 新夥伴們的入門資料,未經授權請勿轉載。 開始之前 閱讀本文前,希望你已能熟練運用 Java 的基礎知識,諸如類、介
動手讀懂 Google 官方範例:Activity 生命週期其實可以「看」見
前幾天終於把 Google Training 裡的 Manage the Activity Lifecycle 這一節認真看完。過去翻書、看影片,對生命週期各個回呼該做什