為android wear創(chuàng)建通知
用NotificationCompat.Builder創(chuàng)建一個可以在手機上發(fā)送到可android wear通知.當用這個類創(chuàng)建通知時,系統(tǒng)負責在合適的時候在手機或者wear上展示這個通知
注意:對于使用RemoteViews來創(chuàng)建布局的通知在wear上僅僅顯示文本和圖標.但是你可以使用自定義的卡片布局創(chuàng)建自定的通知應用運行在wear上.
導入必須的類
在build.gradle
文件中添加下面這個依賴添加必須的引用包
compile "com.android.support:support-v4:20.0.+"
在導入必要的依賴庫之后就有權限訪問一些包,可以導入以下類
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationManagerCompat;
import android.support.v4.app.NotificationCompat.WearableExtender;
使用Notification Builder創(chuàng)建通知
上面導入的v4庫允許你創(chuàng)建一個有新特性(操作按鈕,大圖標)的通知,同時兼容android1.6及更高版本.
創(chuàng)建一個NotificationCompat.Builder實例,使用notify()方法來展示一個通知.
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());
當通知到達手機時,用戶可以通過點擊通知獲取通過setContentIntent()該方法設置的PendingIntent.當在wear上時通過向左滑動通知,來打開這個PendingIntent.
添加操作按鈕
除了通過setContentIntent()
設置主要內容外,還可以通過addAction()為PendingIntent
添加其他行為.
下面的代碼展示一個相同的通知,但是添加了一個操作在地圖上查看事件.
// 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);
在手機上,這個操作像是一個附加在通知上的附加按鈕.在wear上當用戶向左滑動通知時這個操作像是一個大按鈕.當用戶觸碰這個操作,此時這個關聯(lián)的PendingIntent
會顯示.
提示:如果你的通知包含一個回復操作(比如消息應用),你可以通過wear的語音輸入回復來增強行為.更多信息操作這里
wear上不一樣的操作
如果你想wear上顯示的行為操作和手機上的不一樣,那么可以使用WearableExtender.addaction()這個方法.一旦使用了這個方法,在wear上不會在顯示通過NotificationCompat.Builder.addAction()添加的行為操作.也就是說只有通過WearableExtender.addaction()
添加的行為操作會顯示在wear上不會顯示在手機上.
// 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();
添加一個大視圖
通過向通知中添加一個大視圖樣式,可以在通知中插入擴展的文字內容.在手機上用戶可以通過擴展的通知看到這個內容.在wear上這個大視圖內容默認時顯示的.
通過NotificationCompat.Builder的對象調用setStyle()方法添加擴展的內容.該方法可以傳遞BigTextStyle或者InboxStyle的對象實例
舉個例子,下面的代碼向通知中添加一個 NotificationCompat.BigTextStyle實例對象,以便包括完整的事件描述
// 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(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);
注意你可以使用setLargeIcon方法添加一個大圖片到任何一個通知中,然而這些圖標以大背景圖展示在wear上而且不太好看因為這些圖片被放大適應wear的屏幕尺寸.添加一個特殊的圖片到通知上可以參考Add Wearable Features For a Notification,更多關于設計大圖標的通知可以看這里
為通知添加可穿戴的特征
如果你想為你的通知添加可穿戴設備的特征,比如指定其他內容頁面或者讓用戶通過語音輸入指定文字回復.你可以使用NotificationCompat.WearableExtender這個類.
- 創(chuàng)建一個WearableExtender實例對象,給通知設置可穿戴設備的特征.
- 實例化一個NotificationCompat.Builder對象,按照上面所描述設置所需屬性
- 調用extend()方法,傳遞一個WearableExtender對象.這就使得通知具有可穿戴特性.
- 調用build()去構建通知.
舉個例子,下面的代碼調用setHintHideIcon()方法從通知中去掉應用程序圖標.
// 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()
這兩個方法僅僅是和NotificationCompat.WearableExtender有關的兩個可見的通知新特性.
Note: The bitmap that you use with setBackground() should have a resolution of 400x400 for non-scrolling backgrounds and 640x400 for backgrounds that support parallax scrolling. Place these bitmap images in the res/drawable-nodpi directory. Place other non-bitmap resources for wearable notifications, such as those used with the setContentIcon() method, in the res/drawable-hdpi directory.
這點提示沒有看懂,好像是說對于400*400分辨率的圖不能滾動,640*400的可以滾動.
如果你以后需要使用可穿戴特性特定選項,你可以使用特定屬性的get方法.下面這個例子調用getHintHideIcon()
方法獲取是否隱藏了通知的圖標.
NotificationCompat.WearableExtender wearableExtender =
new NotificationCompat.WearableExtender(notif);
boolean hintHideIcon = wearableExtender.getHintHideIcon();
Deliver the Notification (交付通知)
當希望交付傳遞一個通知時,需要用NotificationManagerCompat代替NotificationManager
// Get an instance of the NotificationManager service
NotificationManagerCompat notificationManager =
NotificationManagerCompat.from(mContext);
// Issue the notification with notification manager.
notificationManager.notify(notificationId, notif);
如果使用framework層的NotificationManager,那么一些NotificationCompat.WearableExtender的特性不能使用.