默认情况下,当Android设备(手机,平板)和Android Wear 连接的时候,所有的通知会和这两种
设备共享。在Android Wear上每个通知都会作为独立的一条消息显示在主消息列表中。所以不费任何力气
,你应用的通知已经能显示在你的Android Wear设备上了。然而,你可以通过一些方式来增强用户的体验。
例如,用户反映一个通知需要进行回复,比如回复一条短信,你可以在Android Wear 上增加一个功能,让
用户直接用语音进行回复。
为了使你的通知在Android Wear 上有一个很好用户体验,这篇文章会教你使用NotificationCompat里API
来创建通知的标准模板,还有如何为了用户的体验来扩展你的通知的功能。
注意:通知所用到的RmoteViews对象去除了自定义布局的功能,系统仅使用通知中的文本和图标显示在
通知卡中。自定义页面布局会在即将发布的官方Android Wear SDK中支持。
导入所需要类
在开始开发之前,你必须已经做完了在Get Started with the Developer Preview文章中的指导。就像文章中所
提到的那样,你必须先要添加v4包和Preview support包。写代码之前你必须包含以下导入类的代码:
import android.preview.support.wearable.notifications.*; import android.preview.support.v4.app.NotificationManagerCompat; import android.support.v4.app.NotificationCompat;用Notification Builder 创建 Notification
V4 包允许你用最新通知的功能来创建通知,例如,动作按钮,大图标等。同时和Android1.6以上的半都兼容。
举个例子,下面的代码使用Notification API 和新加的NotificationManagerCompat API创建和发送通知。
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。当通知显示在
Android Wear 设备上时,用户可以通过向左滑动通知让OPen按钮显示出来,触摸该按钮可以在手持设备激活
意图。
添加功能按钮
除了使用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);在手持设备上,这个动作是通知上的一个附加按钮。在Android Wear 上这个动作是一个大的图片按钮,
当用户向左滑动通知的时候,会显示出来。当用点击这个动作按钮的时候,它所关联的Intent会在手持设备上
被调用。
小记:假如你的通知包括一个“Reply”的操作(例如一个消息类的应用),你可以在Android Wear上增加
语音直接回复功能,要想得到更多的信息,请读:
http://developer.android.com/wear/notifications/remote-input.html
对于动作按钮的详细设计(包括突变的规格),请看http://developer.android.com/wear/design/index.html#NotifictionActions
添加大的视图
你可以通过在你通知中添加“Big View”类型来在通知中插入额外的信息内容。在手持设备上,用户可以通过扩展
通知界面来浏览详细信息。在Android Wear 上,大视图的内容默认就是可见的。为了给你的通知添加额外的内容,
调用NotificationCompat.Builder对象的setStyle()方法,给它传一个BigTextStyle或者InboxStyle的实例。
例如,下面代码给事件通知添加了一个NotificationCompat.BigTextStyle的实例,为了包括一个完整的
事件描述(这种通知包括了更多的内容,超出了setContentText()所提供的空间)。
// 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);你可以用setLargeIcon()为任何通知添加大的背景图片。关于如何设计通知背景图片的更多资料,请看:
http://developer.android.com/wear/design/index.html#Images
在Android Wear上添加新功能
Android Wear preview support library 提供了很多新的API,使你可以为Android Wear上的通知扩展用户
体验。例如你可以为详细信息提供其他页面,用户可以向左滑动来浏览它,或者为用户添加一个语音回复功能。
要使用新的API,首先要把NotificationCompat.Builder实例作为参数传给WearableNotifications.Builder()的构造
方法。你可以使用WearableNotifications.Builder的方法为你的通知添加新功能。例如:
// Create a NotificationCompat.Builder for standard notification features NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(mContext) .setContentTitle("New mail from " + sender.toString()) .setContentText(subject) .setSmallIcon(R.drawable.new_mail); // Create a WearablesNotification.Builder to add special functionality for wearables Notification notification = new WearableNotifications.Builder(notificationBuilder) .setHintHideIcon(true) .build();setHintHideIcon()方法用来删除通知卡上的应用图标。这个方法只是一个演示,所有通知的新功能都来自于
WearableNotifications.Builder类。在发送你的通知的时候,要使用NotificationManagerCompat:
// Get an instance of the NotificationManager service NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this); // Build the notification and issues it with notification manager. notificationManager.notify(notificationId, notification);假如你使用的是框架层的NotificationManager,那么WearableNotifications.Builder中新功能是不起作用的。