Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android Wear 開發入門:打造手機與手錶聯動的通知

編輯:關於 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 觸發它,也可以向左滑動將其消除。

二、添加 Action Button

除了主要內容的行為,你還可以通過 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();

四、添加 Big View(大視圖)

開發者可以往通知中插入一段「大視圖」內容。在手機上,用戶需展開通知才能看到;而在穿戴設備上,這段內容默認就是可見的。使用 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,步驟如下:

  1. 創建 WearableExtender 實例並設置穿戴設備特有的屬性;
  2. 創建 NotificationCompat.Builder 並按前文所述設置內容;
  3. 調用 extend() 傳入 WearableExtender;
  4. 調用 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 提供了豐富的可穿戴專屬能力。善用這些特性,你就能為用戶打造真正「戴在手腕上」的優質體驗。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved