編輯:關於 Android 程式設計
可穿戴設備的興起,讓「通知」不再局限於手機屏幕。在 Android Wear 中,一條精心設計的通知可以在手機與手錶上無縫呈現。本文將帶你了解如何創建、擴展並發送能在手機與穿戴設備間聯動的通知。
要在手機與可穿戴設備中都能展現通知,最推薦使用 NotificationCompat.Builder。通過它創建的通知,系統會自動判斷並決定是否在手機或穿戴設備上展示。
首先需要導入相關類庫:
import android.support.v4.app.NotificationCompat; import android.support.v4.app.NotificationManagerCompat; import android.support.v4.app.NotificationCompat.WearableExtender;
v4 支持庫讓開發者能用上最新的通知特性(如 action button、large icon),編譯環境需在 4 以上。基本用法如下:
int notificationId = 001;
// 為通知內容構建 intent
Intent viewIntent = new Intent(this, ViewEventActivity.class);
viewIntent.putExtra(EXTRA_EVENT_ID, eventId);
PendingIntent viewPendingIntent = PendingIntent.getActivity(this, 0, viewIntent, 0);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_event)
.setContentTitle(eventTitle)
.setContentText(eventLocation)
.setContentIntent(viewPendingIntent);
// 獲取 NotificationManager 服務
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
// 發出通知
notificationManager.notify(notificationId, notificationBuilder.build());
當通知出現在手機上時,用戶可通過 setContentIntent() 指定的 PendingIntent 觸發它,也可以向左滑動將其消除。
除了主要內容的行為,你還可以通過 addAction() 傳入 PendingIntent 添加額外操作。例如為通知添加一個「查看地圖」的按鈕:
// 為查看地圖的動作構建 intent
Intent mapIntent = new Intent(Intent.ACTION_VIEW);
Uri geoUri = Uri.parse("geo:0,0?q=" + Uri.encode(location));
mapIntent.setData(geoUri);
PendingIntent mapPendingIntent = PendingIntent.getActivity(this, 0, mapIntent, 0);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_event)
.setContentTitle(eventTitle)
.setContentText(eventLocation)
.setContentIntent(viewPendingIntent)
.addAction(R.drawable.ic_map, getString(R.string.map), mapPendingIntent);
在手機上,該 action 以按鈕形式附在通知旁;而在穿戴設備上,它會呈現為一個大按鈕,點擊後即會在手機中調起對應內容。
如果希望穿戴設備上的行為有別於手機,可使用 WearableExtender.addAction()。使用該方法後,穿戴設備將只展示通過它添加的 action,而不再顯示 NotificationCompat.Builder.addAction() 添加的內容。
// 為回覆動作構建 intent
Intent actionIntent = new Intent(this, ActionActivity.class);
PendingIntent actionPendingIntent = PendingIntent.getActivity(this, 0, actionIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
// 構建 action
NotificationCompat.Action action = new NotificationCompat.Action.Builder(
R.drawable.ic_action, getString(R.string.label), actionPendingIntent).build();
// 通過 WearableExtender 添加 action
Notification notification = new NotificationCompat.Builder(mContext)
.setSmallIcon(R.drawable.ic_message)
.setContentTitle(getString(R.string.title))
.setContentText(getString(R.string.content))
.extend(new WearableExtender().addAction(action))
.build();
開發者可以往通知中插入一段「大視圖」內容。在手機上,用戶需展開通知才能看到;而在穿戴設備上,這段內容默認就是可見的。使用 setStyle() 傳入 BigTextStyle 或 InboxStyle 即可實現:
// 指定大視圖內容,展示可能超出普通文本的長描述
BigTextStyle bigStyle = new NotificationCompat.BigTextStyle();
bigStyle.bigText(eventDescription);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_event)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.notif_background))
.setContentTitle(eventTitle)
.setContentText(eventLocation)
.setContentIntent(viewPendingIntent)
.addAction(R.drawable.ic_map, getString(R.string.map), mapPendingIntent)
.setStyle(bigStyle);
若想加入語音輸入等穿戴設備特有的功能,可借助 NotificationCompat.WearableExtender,步驟如下:
extend() 傳入 WearableExtender;build() 生成通知。下面的代碼用 setHintHideIcon() 移除通知卡片中的圖標:
// 創建 WearableExtender 以添加穿戴設備功能
NotificationCompat.WearableExtender wearableExtender = new NotificationCompat.WearableExtender()
.setHintHideIcon(true);
Notification notif = new NotificationCompat.Builder(mContext)
.setContentTitle("New mail from " + sender)
.setContentText(subject)
.setSmallIcon(R.drawable.new_mail)
.extend(wearableExtender)
.build();
若想在後續讀取這些屬性的值,可以這樣寫:
NotificationCompat.WearableExtender wearableExtender = new NotificationCompat.WearableExtender(notif); boolean hintHideIcon = wearableExtender.getHintHideIcon();
發送時,請使用 NotificationManagerCompat 而非 NotificationManager,否則部分 WearableExtender 的特性將無法生效:
// 獲取 NotificationManager 服務 NotificationManagerCompat notificationManager = NotificationManagerCompat.from(mContext); // 發出通知 notificationManager.notify(notificationId, notif);
可以看到,Android Wear 的通知體系既延續了手機通知的用法,又通過 WearableExtender 提供了豐富的可穿戴專屬能力。善用這些特性,你就能為用戶打造真正「戴在手腕上」的優質體驗。
Android 模擬定位實戰:從權限設定到 GPS 位置注入
這篇筆記整理了 Android 模擬位置的使用方法,支援到 Android 6.0,供大家參考。 一、開啟系統的模擬位置 Android 6.0 以下:進入【開發者選項
Android 緩存實戰:二級緩存概念與輕量框架 ACache
一、為什麼需要緩存 Android 開發的本質,很大程度上就是手機與遠端伺服器之間的資料往返。反覆透過網路取資料的代價很高,一旦請求頻繁,效能與流量都會同時惡化。 解法是
Android Service 啟動流程源碼解析
Service 不是背景執行緒 Service 雖然常用於背景任務,但它預設執行在主執行緒。若直接執行長時間運算,仍可能阻塞 UI 或造成 ANR。若需要耗時工作,應在
Android 開發技巧:啟動屏與新功能引導頁的邏輯設計
寫作停了一陣子,趁工作之餘把一些還算有時效性的實作整理出來分享。這次要談的是幾乎每個 App 都會有的兩個畫面:啟動屏(Splash)與新功能引導頁。它們看起來簡單,但「