注:本文内容来自: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 RemoteViewsare 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() 定义,你可以通过一个PendingIntentaddAction()方法添加其他动作。

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对象中,通过 BigTextStyleInboxStyle的实例调用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:

  1. Create an instance of a WearableExtender, setting the wearable-specific options for the notication.
  2. Create an instance of NotificationCompat.Builder, setting the desired properties for your notification as described earlier in this lesson.
  3. Call extend() on the notification and pass in the WearableExtender. This applies the wearable options to the notification.
  4. Call build() to build the notification.
  1. 创建一个 WearableExtender的实例,来为通知设置特定于可穿戴设备的选项。
  2. 创建一个NotificationCompat.Builder的实例,为你的通知设置一个期望的特性,正如本文早些时候所描述的一样。
  3. 通过WearableExtender在通知中调用extend()。这会向通知中应用可穿戴设备的选项。
  4. 调用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 NotificationManagerCompatAPI 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 >

下一篇文章:在通知中接收语音输入

android wear开发:为可穿戴设备创建一个通知 - Creating a Notification for Wearables的更多相关文章

  1. android wear开发之:增加可穿戴设备功能到通知中 - Adding Wearable Features to Notifications

    注:本文内容来自:https://developer.android.com/training/wearables/notifications/index.html 翻译水平有限,如有疏漏,欢迎批评指 ...

  2. Android Wear 开发入门

    大家好,我是陆嘉杰,我是一名Android开发者.我想和大家进行一些技术交流,希望越来越多的人能和我成为好朋友. 大家都知道,智能手表是下一个开发的风口,而这方面的技术又属于前沿,所以和大家分享下An ...

  3. 想做Android Wear开发?你得先搞明白这四件事

    手环和手表的腕上穿戴之争,随着Apple Watch发布和Android Wear不断完善而告一段落.尽管续航上略有缺陷,但手表以其类似手机可扩展的生态环境赢得了众多巨头的支持. Google曾透露, ...

  4. Android Wear创建一个通知

    创建Android Wear的通知实际上和手机上创建没啥区别,主要是多了几个新类,只要用熟悉了一切都好办了.(如果只是测试通知,则直接运行wear app就能够看到效果) 创建一个简单的wear通知分 ...

  5. IDEA搭建Android wear开发环境,Android wear,I'm comming!

    随着google公布了android wear这个东西.然后又有了三星的gear,LG的G watch以及moto 360,苹果由公布了apple watch.未来可能在智能手表行业又有一场战争. 当 ...

  6. Android TV开发总结(三)构建一个TV app的焦点控制及遇到的坑

    转载请把头部出处链接和尾部二维码一起转载,本文出自逆流的鱼yuiop:http://blog.csdn.net/hejjunlin/article/details/52835829 前言:上篇中,&l ...

  7. Android TV开发总结(六)构建一个TV app的直播节目实例

    请尊重分享成果,转载请注明出处:http://blog.csdn.net/hejjunlin/article/details/52966319 近年来,Android TV的迅速发展,传统的有线电视受 ...

  8. Android TV开发总结(七)构建一个TV app中的剧集列表控件

    原文:Android TV开发总结(七)构建一个TV app中的剧集列表控件 版权声明:我已委托"维权骑士"(rightknights.com)为我的文章进行维权行动.转载务必转载 ...

  9. Android Wear 开发入门——怎样创建一个手机与可穿戴设备关联的通知(Notification)

    创建通知 为了创建在手机与可穿戴设备中都能展现的通知,能够使用 NotificationCompat.Builder.通过该类创建的通知,系统会处理该通知是否展如今手机或者穿戴设备中. 导入必要的类库 ...

随机推荐

  1. 【转】TCP/IP报文格式

    1.IP报文格式 IP协议是TCP/IP协议族中最为核心的协议.它提供不可靠.无连接的服务,也即依赖其他层的协议进行差错控制.在局域网环境,IP协议往往被封装在以太网帧(见本章1.3节)中传送.而所有 ...

  2. ZOJ2110 HDU1010 搜索 Tempter of the Bone

    传送门:Tempter of the Bone 大意是给一个矩阵,叫你是否可以在给定的可走路径上不重复地走,在最后一秒走到终点. 我用了两个剪枝,且称其为简直001和剪枝002,事实证明001不要都可 ...

  3. 【MVC】会员注册/登录,普通验证,会员名是否注册Ajax验证以及会员邮件验证实现原理

    using System; using System.Collections.Generic; using System.ComponentModel; using System.ComponentM ...

  4. iOS开发之AutoLayout中的Content Hugging Priority和 Content Compression Resistance Priority解析

    本篇博客的内容也不算太复杂,算是AutoLayout的一些高级的用法.本篇博客我们主要通过一些示例来看一下AutoLayout中的Content Hugging Priority以及Content C ...

  5. MSDN-9月杂志推荐

    序言:MSDN Magazine频道每月都会推出杂志,提供给广大开发者免费阅读,文章的作者往往都是国内外技术大牛.而且翻译良好(机器翻译除外)排班工整,是技术进阶的绝美助力! 本系列文章会关注MSDN ...

  6. php 守护进程类

    最近个人项目中需要后台运行任务,之前一直是用nouhp & + 重定向输出 来后台跑任务,后来觉得不好维护原始数据,同时可能也没有直接操作进程那么稳吧(没验证).废话少说,来看分析. 首先,我 ...

  7. SQL数据库的基础操作

    一,认识SQL数据库 美国Microsoft公司推出的一种关系型数据库系统.SQLServer是一个可扩展的.高性能的.为分布式客户机/服务器计算所设计的数据库管理系统,实现了与WindowsNT的有 ...

  8. 如何升级laravel5.4到laravel5.5并使用新特性?

    如何升级laravel5.4到laravel5.5并使用新特性? 修改composer.json: "laravel/framework": "5.5.*", ...

  9. 阿里JAVA开发手册零度的思考理解(一)

    转载请注明原创出处,谢谢! 缘由 阿里JAVA开发手册已经发表有很长时间了,值得认真研究思考推广 阿里官方的Java代码规范标准,这份开发手册不仅规范了一些开发细节,也提出了很多工程开发的哲学,值得好 ...

  10. LeetCode 339. Nested List Weight Sum (嵌套列表重和)$

    Given a nested list of integers, return the sum of all integers in the list weighted by their depth. ...