android wear开发:为可穿戴设备创建一个通知 - Creating a Notification for Wearables
注:本文内容来自:https://developer.android.com/training/wearables/notifications/creating.html
翻译水平有限,如有疏漏,欢迎批评指教。
译:山人
为可穿戴设备创建一个通知
Creating a Notification for Wearables
To build handheld notifications that are also sent to wearables, use NotificationCompat.Builder
. When you build notifications with this class, the system takes care of displaying notifications properly, whether they appear on a handheld or wearable.
使用 NotificationCompat.Builder 建立的手持设备通知,也将发送到可穿戴设备。当你使用这个类建立通知,系统将正确地处理通知的显示,不论它们是出现在手持设备还是可穿戴设备之上。
Note: Notifications using RemoteViews
are stripped of custom layouts and the wearable only displays the text and icons. However, you can create create custom notifications that use custom card layouts by creating a wearable app that runs on the wearable device.
注: RemoteViews 通知被自定义布局所剥离,并且可穿戴设备只显示文本或图标。但是,你可以运用自定义卡片布局,创建自定义的通知(create custom notifications ),伴随一个可穿戴设备的app运行在可穿戴设备之上。
I. 导入必要的类
I. Import the necessary classes
To import the necessary packages, add this line to your build.gradle
file:
导入必要的包,向 build.gradle 文件中添加这行:
compile "com.android.support:support-v4:20.0.+"
Now that your project has access to the necessary packages, import the necessary classes from the support library:
现在你的项目已经获得了必要的包,从支持库中导入必要的类:
import android.support.v4.app.NotificationCompat; import android.support.v4.app.NotificationManagerCompat; import android.support.v4.app.NotificationCompat.WearableExtender;
II. 创建通知与通知生成器
II. Create Notifications with the Notification Builder
The v4 support library allows you to create notifications using the latest notification features such as action buttons and large icons, while remaining compatible with Android 1.6 (API level 4) and higher.
v4版本支持库 v4 support library 允许你使用诸如action buttons与large icons最新的通知功能创建通知,同时保持与Android 1.6(API level 4)或更高版本的支持。
To create a notification with the support library, you create an instance of NotificationCompat.Builder
and issue the notification by passing it to notify()
. For example:
用支持库创建一个通知,创建 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());
When this notification appears on a handheld device, the user can invoke the PendingIntent
specified by the setContentIntent()
method by touching the notification. When this notification appears on an Android wearable, the user can swipe the notification to the left to reveal the Open action, which invokes the intent on the handheld device.
当这个通知出现在手持设备之上,用户通过触摸通知,可以指定 setContentIntent() 方法调用 PendingIntent 。当通知显示在可穿戴设备之上时,用户可以将通知向左滑动,以打开在手持设备上调用intent的这个动作。
III.增加一个动作按钮
III. Add Action Buttons
In addition to the primary content action defined by setContentIntent()
, you can add other actions by passing a PendingIntent
to the addAction()
method.
除了动作的主要内容会被 setContentIntent() 定义,你可以通过一个PendingIntent的addAction()方法添加其他动作。
For example, the following code shows the same type of notification from above, but adds an action to view the event location on a map.
下面的代码将显示和上面相同类型的通知,但是增加了一个动作,在地图上显示事件的位置。
// 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);
On a handheld, the action appears as an additional button attached to the notification. On a wearable, the action appears as a large button when the user swipes the notification to the left. When the user taps the action, the associated intent is invoked on the handheld.
在手持设备上,该动作作为一个附加按钮来显示。在可穿戴设备上,当用户将通知向左滑动时,该动作显示一个大图标。当用户触碰这个动作,手机上相关联的intent会被调用。
Tip: If your notifications include a "Reply" action (such as for a messaging app), you can enhance the behavior by enabling voice input replies directly from the Android wearable. For more information, read Receiving Voice Input from a Notification.
提示:如果你的通知包含一个“回复”动作(例如一个app消息),你可以从android wearable用语音直接输入答复。更多信息请阅读 Receiving Voice Input from a Notification。
IV. 指定只用于可穿戴设备的动作
IV. Specify Wearable-only Actions
If you want the actions available on the wearable to be different from those on the handheld, then use WearableExtender.addAction()
. Once you add an action with this method, the wearable does not display any other actions added with NotificationCompat.Builder.addAction()
. That is, only the actions added with WearableExtender.addAction()
appear on the wearable and they do not appear on the handheld.
如果你想要wearable的可用动作不同于handheld,那么使用 WearableExtender.addAction() 。一旦使用这个方法添加一个动作,那么wearable将屏蔽其他任何已用 NotificationCompat.Builder.addAction() 添加的动作。同时,只要动作被 WearableExtender.addAction() 添加到可穿戴设备中,那么动作就不会被手持设备所显示。
// 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();
V. 添加一个大的视图
V. Add a Big View
You can insert extended text content to your notification by adding one of the "big view" styles to your notification. On a handheld device, users can see the big view content by expanding the notification. On a wearable device, the big view content is visible by default.
你可以通过添加一个大的视图样式,插入可扩展的文字内容到你的通知中。在手持设备上,用户可以展开通知来看到大的视图内容。在可穿戴设备上,默认一个大视图内容是可见的。
To add the extended content to your notification, call setStyle()
on the NotificationCompat.Builder
object, passing it an instance of either BigTextStyle
or InboxStyle
.
在NotificationCompat.Builder对象中,通过 BigTextStyle或InboxStyle的实例调用setStyle(),来向你的通知中添加可扩展内容。
For example, the following code adds an instance of NotificationCompat.BigTextStyle
to the event notification, in order to include the complete event description (which includes more text than can fit into the space provided for setContentText()
).
例如,下面的代码添加一个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(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);
Notice that you can add a large icon image to any notification using the setLargeIcon()
method. However, these icons appear as large background images on a wearable and do not look good as they are scaled up to fit the wearable screen. To add a wearable-specific background image to a notification, see Add Wearable Features For a Notification. For more information about designing notifications with large images, see the Design Principles of Android Wear.
注意,你可以使用setLargeIcon()方法添加一个大的图标图片到任意通知中。然而,这些图标就像大的背景图片一样显示在可穿戴设备中,它们被拉伸到适应屏幕,同时看起来并不美观。添加一个特定于可穿戴设备的背景图片到通知中,见 Add Wearable Features For a Notification。更多关于设计通知大图的信息,见Design Principles of Android Wear。
VI. 为一个通知添加可穿戴设备的功能
VI. Add Wearable Features For a Notification
If you ever need to add wearable-specific options to a notification, such as specifying additional pages of content or letting users dictate a text response with voice input, you can use the NotificationCompat.WearableExtender
class to specify the options. To use this API:
如果你需要在通知中添加特定于可穿戴设备的选项,例如特别的附加内容页面,或让用户使用语音口述一个文本答复,可以使用NotificationCompat.WearableExtender类来指定选项。使用这个API:
- Create an instance of a
WearableExtender
, setting the wearable-specific options for the notication. - Create an instance of
NotificationCompat.Builder
, setting the desired properties for your notification as described earlier in this lesson. - Call
extend()
on the notification and pass in theWearableExtender
. This applies the wearable options to the notification. - Call
build()
to build the notification.
- 创建一个
WearableExtender的实例,来为通知设置特定于可穿戴设备的选项。
- 创建一个NotificationCompat.Builder的实例,为你的通知设置一个期望的特性,正如本文早些时候所描述的一样。
- 通过WearableExtender在通知中调用
extend()。这会向通知中应用可穿戴设备的选项。
- 调用
build()方法建立一个通知。
For example, the following code calls the setHintHideIcon()
method to remove the app icon from the notification card.
例如,下面的代码调用setHintHideIcon()方法,从通知卡片中移除app图标。
// 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();
The setHintHideIcon()
and setBackground()
methods are just two examples of new notification features available with NotificationCompat.WearableExtender
.
方法 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 thesetContentIcon()
method, in the res/drawable-hdpi
directory.
注意:用setBackground()使用的位图应该是400x400的分辨率且无滚动的背景,并且640x400可滚动背景。这些位图的应位于res/drawable-nodpi目录。其他关于通知的非位图资源的位置,如setContentIcon()方法使用的图标,放在 res/drawable-hdpi目录下。
If you ever need to read wearable-specific options at a later time, use the corresponding get method for the option. This example calls the getHintHideIcon()
method to get whether or not this notification hides the icon:
如果你稍后需要读取特定于可穿戴设备的选项,使用对应选项的get方法。例如调用getHintHideIcon()方法可以获取这个通知是否隐藏图标。
NotificationCompat.WearableExtender wearableExtender = new NotificationCompat.WearableExtender(notif); boolean hintHideIcon = wearableExtender.getHintHideIcon();
VII. 传递通知
VII. Deliver the Notification
When you want to deliver your notifications, always use the NotificationManagerCompat
API instead of NotificationManager
:
当你想要传递你的通知,总是使用NotificationManagerCompat来替代NotificationManager。
// Get an instance of the NotificationManager service NotificationManagerCompat notificationManager = NotificationManagerCompat.from(mContext); // Issue the notification with notification manager. notificationManager.notify(notificationId, notif);
If you use the framework's NotificationManager
, some features fromNotificationCompat.WearableExtender
do not work, so make sure to use NotificationCompat
.
如果你用框架的NotificationManager,一些来自NotificationCompat.WearableExtender无法工作,因此确定要用NotificationCompat。
Next: Receiving Voice Input in a Notification >
下一篇文章:在通知中接收语音输入