W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
編寫:wangyachen - 原文: http://developer.android.com/training/wearables/notifications/creating.html
使用 NotificationCompat.Builder 來創(chuàng)建可以發(fā)送給可穿戴設(shè)備的手持設(shè)備Notification。當(dāng)我們使用這個(gè)類創(chuàng)建Notification之后,無論Notification出現(xiàn)在手持式設(shè)備上還是可穿戴設(shè)備上,系統(tǒng)都會(huì)把Notification正確地顯示出來。
Note:使用 RemoteViews 的Notification會(huì)剝除自定義的 layout,并且可穿戴設(shè)備上只顯示文本和圖標(biāo)。但是,通過創(chuàng)建一個(gè)運(yùn)行在可穿戴設(shè)備上的應(yīng)用,開發(fā)者能夠使用自定義的卡片布局創(chuàng)建自定義Notifications。
為了引入必要的包,在我們的 build.gradle
文件中加入如下內(nèi)容:
compile "com.android.support:support-v4:20.0.+"
現(xiàn)在我們的項(xiàng)目能夠訪問關(guān)鍵的包,接下來從support library中引入必要的類:
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationManagerCompat;
import android.support.v4.app.NotificationCompat.WearableExtender;
v4 support library能夠讓開發(fā)者使用最新的特性去創(chuàng)建 Notification,諸如action 按鈕和大的圖標(biāo),而且兼容Android1.6(API level4)及以上的版本。
為了通過support library創(chuàng)建一個(gè)Notification,我們需要?jiǎng)?chuàng)建一個(gè) NotificationCompat.Builder 的實(shí)例,然后通過將該實(shí)例傳給 notify()) 來發(fā)出 Notification。例如:
int notificationId = 001;
// Build intent for notification content
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);
// Get an instance of the NotificationManager service
NotificationManagerCompat notificationManager =
NotificationManagerCompat.from(this);
// Build the notification and issues it with notification manager.
notificationManager.notify(notificationId, notificationBuilder.build());
當(dāng)該Notification出現(xiàn)在手持設(shè)備上時(shí),用戶能夠通過觸摸Notification來觸發(fā)之前通過setContentIntent()設(shè)置的PendingIntent。當(dāng)該Notification出現(xiàn)在可穿戴設(shè)備上時(shí),用戶能夠通過向左滑動(dòng)該Notification顯示Open的action,點(diǎn)擊這個(gè)action能夠激活手持設(shè)備上的Intent。
除了通過 setContentIntent()) 定義的主要內(nèi)容action之外,我們還可以通過傳遞一個(gè) PendingIntent 給 addAction()) 來添加其它action。
例如,下面的代碼展示了創(chuàng)建一個(gè)同之前相仿的Notification,只不過添加了一個(gè)在地圖上查看事件位置的action。
// Build an intent for an action to view a map
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);
在手持設(shè)備上,action表現(xiàn)為在Notification上附加的一個(gè)額外按鈕。而在可穿戴設(shè)備上,action表現(xiàn)為Notification左滑后出現(xiàn)的大按鈕。當(dāng)用戶點(diǎn)擊action時(shí),能夠觸發(fā)手持設(shè)備上對(duì)應(yīng)的intent。
Tip:如果我們的Notification包含了一個(gè)"回復(fù)"的action(例如短信類app),我們可以通過支持直接從Android可穿戴設(shè)備返回的語音輸入,來加強(qiáng)該功能的體驗(yàn)。更多信息,詳見在Notification中接收語音輸入。
如果開發(fā)者想要可穿戴式設(shè)備上的action與手持式設(shè)備不一樣的話,可以使用 WearableExtender.addAction()),一旦我們通過這種方式添加了action,可穿戴式設(shè)備便不會(huì)顯示任何其他通過 NotificationCompat.Builder.addAction()) 添加的action。這是因?yàn)?,只有通過 WearableExtender.addAction()) 添加的action才能只在可穿戴設(shè)備上顯示且不在手持式設(shè)備上顯示。
// Create an intent for the reply action
Intent actionIntent = new Intent(this, ActionActivity.class);
PendingIntent actionPendingIntent =
PendingIntent.getActivity(this, 0, actionIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
// Create the action
NotificationCompat.Action action =
new NotificationCompat.Action.Builder(R.drawable.ic_action,
getString(R.string.label, actionPendingIntent))
.build();
// Build the notification and add the action via WearableExtender
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();
開發(fā)者可以在Notification中通過添加某種"big view"風(fēng)格來插入擴(kuò)展文本。在手持式設(shè)備上,用戶能夠通過展開Notification看見big view的內(nèi)容。在可穿戴式設(shè)備上,big view內(nèi)容是默認(rèn)可見的。
可以通過 NotificationCompat.Builder 對(duì)象調(diào)用 setStyle()),并設(shè)置參數(shù)為 BigTextStyle 或 InboxStyle 的實(shí)例,從而將擴(kuò)展內(nèi)容添加到 Notification 中。
比如,下面的代碼為事件 Notification 添加了一個(gè) NotificationCompat.BigTextStyle 的實(shí)例,目的是為了包含完整的事件描述(這能夠包含比 setContentText()) 提供的空間所能容納的字?jǐn)?shù)更多的文字)。
// Specify the 'big view' content to display the long
// event description that may not fit the normal content text.
BigTextStyle bigStyle = new NotificationCompat.BigTextStyle();
bigStyle.bigText(eventDescription);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_event)
.setLargeIcon(BitmapFractory.decodeResource(
getResources(), R.drawable.notif_background))
.setContentTitle(eventTitle)
.setContentText(eventLocation)
.setContentIntent(viewPendingIntent)
.addAction(R.drawable.ic_map,
getString(R.string.map), mapPendingIntent)
.setStyle(bigStyle);
要注意的是,開發(fā)者可以通過 setLargeIcon()) 方法為任何 Notification 添加一個(gè)大圖標(biāo)。但是,這些圖標(biāo)在可穿戴設(shè)備上會(huì)顯示成大的背景圖片,并且由于這些圖標(biāo)會(huì)被放大以適應(yīng)可穿戴設(shè)備的屏幕,導(dǎo)致這些圖標(biāo)顯示的效果不好。想要為 Notification 添加一個(gè)可穿戴設(shè)備適用的背景圖片,請(qǐng)看下面一小節(jié)為 Notification 添加可穿戴式特性。更多關(guān)于大圖片在 Notification 上的設(shè)計(jì),詳見 Design Principles of Android Wear。
如果我們需要為 Notification 添加一些可穿戴式的特性設(shè)置,比如制定額外的內(nèi)容頁(yè),或者讓用戶通過語音輸入一些文字,那么我們可以使用 NotificationCompat.WearableExtender 來制定這些設(shè)置。為了適用這個(gè) API,我們需要:
例如,以下代碼調(diào)用 setHintHideIcon()) 方法把應(yīng)用的圖標(biāo)從 Notification 卡片上刪掉。
// Create a WearableExtender to add functionality for wearables
NotificationCompat.WearableExtender wearableExtender =
new NotificationCompat.WearableExtender()
.setHintHideIcon(true)
.setBackground(mBitmap);
// Create a NotificationCompat.Builder to build a standard notification
// then extend it with the WearableExtender
Notification notif = new NotificationCompat.Builder(mContext)
.setContentTitle("New mail from " + sender)
.setContentText(subject)
.setSmallIcon(R.drawable.new_mail)
.extend(wearableExtender)
.build();
setHintHideIcon()) 和 setBackground()) 這兩個(gè)方法是 NotificationCompat.WearableExtender 可用的新 Noticication 特性的兩個(gè)例子。
Note:setBackground()) 中使用的位圖在不滾動(dòng)的背景下應(yīng)該是 400x400 的分辨率,在支持視差滾動(dòng)的背景下應(yīng)該是 640x640。將這些位圖放在
res/drawable-nodpi
目錄下。將可穿戴 Notification 中使用的其它不是位圖的資源放到res/drawable-hdpi
目錄,例如 setContentIcon()) 用到的那些資源。
如果開發(fā)者需要稍后去讀取可穿戴特性的設(shè)置,可以使用設(shè)置相應(yīng)的get方法,該例子通過調(diào)用 getHintHideIcon()) 去獲取當(dāng)前 Notification 是否隱藏了圖標(biāo)。
NotificationCompat.WearableExtender wearableExtender =
new NotificationCompat.WearableExtender(notif);
boolean hintHideIcon = wearableExtender.getHintHideIcon();
如果開發(fā)者想要傳遞自己的 Notification,請(qǐng)使用 NotificationManagerCompat 的API代替 NotificationManager:
// Get an instance of the NotificationManager service
NotificationManagerCompat notificationManager =
NotificationManagerCompat.from(mContext);
// Issue the notification with notification manager.
notificationManager.notify(notificationId, notif);
如果開發(fā)者使用了framework中的 NotificationManager ,那么 NotificationCompat.WearableExtender 中的一些特性就會(huì)失效,所以,請(qǐng)確保使用 NotificationManagerCompat。
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: