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 >
下一篇文章:在通知中接收语音输入
android wear开发:为可穿戴设备创建一个通知 - Creating a Notification for Wearables的更多相关文章
- android wear开发之:增加可穿戴设备功能到通知中 - Adding Wearable Features to Notifications
注:本文内容来自:https://developer.android.com/training/wearables/notifications/index.html 翻译水平有限,如有疏漏,欢迎批评指 ...
- Android Wear 开发入门
大家好,我是陆嘉杰,我是一名Android开发者.我想和大家进行一些技术交流,希望越来越多的人能和我成为好朋友. 大家都知道,智能手表是下一个开发的风口,而这方面的技术又属于前沿,所以和大家分享下An ...
- 想做Android Wear开发?你得先搞明白这四件事
手环和手表的腕上穿戴之争,随着Apple Watch发布和Android Wear不断完善而告一段落.尽管续航上略有缺陷,但手表以其类似手机可扩展的生态环境赢得了众多巨头的支持. Google曾透露, ...
- Android Wear创建一个通知
创建Android Wear的通知实际上和手机上创建没啥区别,主要是多了几个新类,只要用熟悉了一切都好办了.(如果只是测试通知,则直接运行wear app就能够看到效果) 创建一个简单的wear通知分 ...
- IDEA搭建Android wear开发环境,Android wear,I'm comming!
随着google公布了android wear这个东西.然后又有了三星的gear,LG的G watch以及moto 360,苹果由公布了apple watch.未来可能在智能手表行业又有一场战争. 当 ...
- Android TV开发总结(三)构建一个TV app的焦点控制及遇到的坑
转载请把头部出处链接和尾部二维码一起转载,本文出自逆流的鱼yuiop:http://blog.csdn.net/hejjunlin/article/details/52835829 前言:上篇中,&l ...
- Android TV开发总结(六)构建一个TV app的直播节目实例
请尊重分享成果,转载请注明出处:http://blog.csdn.net/hejjunlin/article/details/52966319 近年来,Android TV的迅速发展,传统的有线电视受 ...
- Android TV开发总结(七)构建一个TV app中的剧集列表控件
原文:Android TV开发总结(七)构建一个TV app中的剧集列表控件 版权声明:我已委托"维权骑士"(rightknights.com)为我的文章进行维权行动.转载务必转载 ...
- Android Wear 开发入门——怎样创建一个手机与可穿戴设备关联的通知(Notification)
创建通知 为了创建在手机与可穿戴设备中都能展现的通知,能够使用 NotificationCompat.Builder.通过该类创建的通知,系统会处理该通知是否展如今手机或者穿戴设备中. 导入必要的类库 ...
随机推荐
- 关于JQuery全选/反选第二次失效的问题
最近在项目中,遇到一个问题,测试全选/反选功能时,第一次对母框进行选中/非选中时,能同步子框的全选/反选状态,之后再点击母框,子框就没反应了.原代码大致结构关键如下: function selectA ...
- PHP字符串替换str_replace()函数4种用法详解
mixed str_replace ( mixed $search , mixed $replace , mixed $subject [, int &$count ] )该函数返回一个字符串 ...
- Xp下麦克风设备及音量检测
从Vista开始,windows底层的音频架构发生了改变:原本是底层API的waveXXX.mixerXXX等都在Core Audio APIs的基础上进行了重构,上升为了高层API:底层API变为C ...
- akka源码导读
akka的actor模型提供了强大的并发,本人就akka源码进行了详细的阅读,下面是一些体会. 1.object SystemMessageList: @tailrec private[sysmsg] ...
- 【特效】单选按钮和复选框的美化(只用css)
表单的默认样式都是比较朴素的,实际页面中往往需要美化他们.这里先说说单选按钮和复选框,有了css3,这个问题就变的好解决了.利用input与label相关联,对label进行美化并使其覆盖掉原本的in ...
- 笨鸟先飞之ASP.NET MVC系列之过滤器(04认证过滤器过滤器)
概念介绍 认证过滤器是MVC5的新特性,它有一个相对复杂的生命周期,它在其他所有过滤器之前运行,我们可以在认证过滤器中创建一个我们定义的认证方法,也可以结合授权过滤器做一个复杂的认证方法,这个方法可以 ...
- Java集合框架,未完
一.集合类 集合的由来: 面向对象语言对事物的体现都是以对象的形式,为了方便对多个对象的操作,就需要将对象进行存储,集合就是存储对象最常用的一种方式. 集合特点:1,用于存储对象的容器.(容器本身就是 ...
- python 目录文件
每天写一点,总有一天我这条咸鱼能变得更咸 python 中对文件及目录的操作基本依赖与os,shutil模块,其中以os模块为主,最主要的几个方法实例如下: 1.判断文件/目录是否存在(os.path ...
- 华为笔试题--LISP括号匹配 解析及源码实现
在17年校招中3道题目AC却无缘华为面试,大概是华为和东华互不待见吧!分享一道华为笔试原题,共同进步! ************************************************ ...
- Loadrunner Http接口Get/Post方法性能测试脚本解析
最近使用LoadRunner 11进行了一次完整的Http WEB接口性能测试,下面介绍下Http接口Get/Post方法性能测试脚本通用编写方法. 1. Http接口性能测试基本流程 首先定义了一个 ...