編輯:Android 資訊
幾乎每個 Android 應用都會用到 Notification,它能在符合規則的時機向使用者推送訊息,是非常實用的功能。本文整理四種常見用法,並釐清其中幾個容易踩坑的細節。
發出通知的一般步驟:
Notification 常見的用途如下圖:

若有多則內容相同的通知,為了避免通知列表被塞滿,可以做成計數堆疊,只顯示一條並累加次數:
btn1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { count++; //從系統服務中獲得通知管理器 NotificationManager nm=(NotificationManager)MainActivity.this.getSystemService(Context.NOTIFICATION_SERVICE); //定義notification Notification n=new Notification(R.drawable.ic_launcher,"我是通知提示",System.currentTimeMillis()); n.flags=Notification.FLAG_AUTO_CANCEL; n.number=count; //通知消息與Intent關聯 Intent it=new Intent(MainActivity.this,Second.class); it.putExtra("name", "name:"+count); PendingIntent pi=PendingIntent.getActivity(MainActivity.this, 100, it, PendingIntent.FLAG_CANCEL_CURRENT); //具體的通知內容 n.setLatestEventInfo(MainActivity.this, "標題", "啟動多次通知只顯示一條",pi); //執行通知 nm.notify(1, n); } });
點擊四次後的效果如下:

這是因為 nm.notify(1, n) 的 id 固定為 1,系統便把它視為同一則通知而不斷覆寫。
btn2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { count++; //從系統服務中獲得通知管理器 NotificationManager nm=(NotificationManager)MainActivity.this.getSystemService(Context.NOTIFICATION_SERVICE); //定義notification Notification n=new Notification(R.drawable.ic_launcher,"我是通知提示",System.currentTimeMillis()); n.flags=Notification.FLAG_AUTO_CANCEL; n.number=count; //通知消息與Intent關聯 Intent it=new Intent(MainActivity.this,Second.class); it.putExtra("name", "name:"+count); PendingIntent pi=PendingIntent.getActivity(MainActivity.this, 100, it, PendingIntent.FLAG_CANCEL_CURRENT); //具體的通知內容 n.setLatestEventInfo(MainActivity.this, "標題", "啟動多次通知顯示多條,但只能用最新的通知跳轉到活動",pi); //執行通知 nm.notify(count, n); } });
結果如下:

點第五、第六則通知都不會跳轉,只有第七則可以。原因是 PendingIntent 的 requestCode 相同——系統會視為同一個 PendingIntent,因此只有最後一次建立的那個有效。
btn3.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { count++; //從系統服務中獲得通知管理器 NotificationManager nm=(NotificationManager)MainActivity.this.getSystemService(Context.NOTIFICATION_SERVICE); //定義notification Notification n=new Notification(R.drawable.ic_launcher,"我是通知提示",System.currentTimeMillis()); n.flags=Notification.FLAG_AUTO_CANCEL; n.number=count; //通知消息與Intent關聯 Intent it=new Intent(MainActivity.this,Second.class); it.putExtra("name", "name:"+count); PendingIntent pi=PendingIntent.getActivity(MainActivity.this, count, it, PendingIntent.FLAG_CANCEL_CURRENT); //具體的通知內容 n.setLatestEventInfo(MainActivity.this, "標題", "啟動多次通知顯示多條,每一條通知都能跳轉到對應的活動",pi); //執行通知 nm.notify(count, n); } });
關鍵差別在於把 PendingIntent 的 requestCode 換成 count,每次都不同,等於為每則通知配了一個獨立的 PendingIntent。畫面呈現與上一張圖相同,但每一則都能正確跳轉到對應的活動。
下圖中的第一則通知就是自訂的:

自訂版面與一般做法唯一的差別,是多載入了一個 RemoteViews:
RemoteViews rv=new RemoteViews(MainActivity.this.getPackageName(),R.layout.notify); n.contentView=rv; //通知消息與Intent關聯 Intent it=new Intent(MainActivity.this,Second.class); it.putExtra("name", "name:"+count); PendingIntent pi=PendingIntent.getActivity(MainActivity.this, count, it, 0); n.contentIntent=pi;
原本想再做一個「點擊按鈕關閉通知」的效果,思路如下。
RemoteViews 沒有類似 findViewById 的方法,因此無法直接取得自訂版面中的元件,也就無法為它掛上點擊事件。不過 PendingIntent 提供了多種取得方式:

可以看到它能關聯 Activity、Service 與 BroadcastReceiver 三類元件。另外,RemoteViews 雖然沒有 findViewById,卻提供了 setOnClickFillInIntent 與 setOnClickPendingIntent 兩個方法。
於是先註冊一個廣播:
//注冊廣播 BroadcastReceiver b=new BroadcastReceiver(){ @Override public void onReceive(Context context, Intent intent) { Toast.makeText(context, "button被點擊了", 3000).show(); } }; IntentFilter filter=new IntentFilter(); filter.addAction("ONCLICK"); this.registerReceiver(b, filter);
接著在 nm.notify 之後設定元件事件:
//執行通知 nm.notify(count, n); //設置控件 rv.setTextViewText(R.id.tv2, "我是自定義布局"); Intent clickIntent=new Intent("ONCLICK"); PendingIntent clickPending=PendingIntent.getBroadcast(MainActivity.this, count, clickIntent, 0); rv.setOnClickPendingIntent(R.id.cancel, clickPending);
結果並沒有生效,廣播完全收不到。反覆檢查程式碼並未發現問題,查了 setOnClickPendingIntent 的說明才發現,它對 ListView 等元件的事件是不起作用的。而 setOnClickFillInIntent 看起來可行,官方另有一個類似的方法,但當時的 API level 不足無法使用,也就沒有實際驗證。難怪市面上那些精緻的通知樣式都要花一番功夫,這部分留待日後有需求時再深入研究。
參考程式碼:https://download.csdn.net/detail/kkkkkxiaofei/6723903
用 Fragment 打造萬能的頁面切換框架
先回顧一下傳統的 Activity 跳轉方式:新建 Intent、塞入必要參數、調用 startActivity。如果需要返回結果,就改用 startActivityFo
用 include 標籤拆分 Android 佈局,讓 XML 不再臃腫
做 Android 開發時,幾乎每個人都遇過這樣的場景:好幾個頁面的頂部標題欄、底部功能列長得一模一樣,卻被反覆複製到各個佈局檔案裡。結果就是 XML 動輒幾百行,改一個
Android 換膚方案實作:把皮膚做成獨立 APK 動態載入
這篇筆記分享一個 Android 換膚的 Demo 思路與完整實作,程式碼已整理上傳到我的 GitHub,後續文章涉及的程式碼也都會統一放在那裡管理。 GitHub 位址
Android APK 簽名原理與過程詳解
在 Android 系統裡,所有安裝進系統的應用都必須帶有一個數位憑證,用來標識應用的作者,並在應用之間建立信任關係。若某個 permission 的 protectio