创建通知

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

导入必要的类库

在开发之前首先须要导入下面类库

importandroid.support.v4.app.NotificationCompat;
importandroid.support.v4.app.NotificationManagerCompat;
importandroid.support.v4.app.NotificationCompat.WearableExtender;

使用Notification Builder创建通知

v4 support library 库同意开发人员使用最新的通知特性,如action button或者large
icons,编译环境须要在4以上。

使用支持库开发的通知,须要创建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.BuildernotificationBuilder=
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());

当这条通知出如今手机上是,使用者能够指定PendingIntent通过使用setContentIntent()方法触发这条通知,当通知出如今手机中时,使用者何以向左滑动来销毁出现的通知。

加入Action Button

除了定义的setcontentintent()主要内容的行为,你能够通过传递一个PendingIntent到addaction()方法加入其它行动。

比如,以下的代码展示了与上面代码同样类型的通知,可是加入了一个动作在view的上面。

// 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);

在手机上,该action是以button形式附加在notification一起,在穿戴设备中,该action是以一个大button形式出现的,当用户单击该button,intent中的内容将在手机中被调起。

指定可穿戴设备独有的行为

假设希望可穿戴设备中的行为有别于手机,使用WearableExtender.addAction().当使用该方法,穿戴设备中将不再展现使用NotificationCompat.Builder.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();

加入Big View

开发人员能够插入一个Big View形式的内容进入通知,在手机中,用户能够通过展开通知来查看该Big View,在可穿戴设备中,该Big View是默认可见的。

加入这样的可展开的内容作为通知,使用 NotificationCompat.Builder 对象中的setStyle() 方法,传递给实例BigTextStyle 或者 InboxStyle样式。

比如以下代码是加入了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(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);

为通知加入可穿戴设备特性

假设想加入可穿戴设备中特有的特性,如语音输入等,能够使用NotificationCompat.WearableExtender 类,使用该类分下面几步。

1 创建WearableExtender实例,设置可穿戴设备特有的特性

2 创建NotificationCompat.Builder实例,设置如前文所述

3 调用extend()方法,传递给WearableExtender

4 调用build()创建通知

以下代码使用 setHintHideIcon()移除通知卡片中的图标

// Create a WearableExtender to add functionality for wearables
NotificationCompat.WearableExtender wearableExtender =
new NotificationCompat.WearableExtender()
.setHintHideIcon(true); // 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();

假设想在后面读取特有的特性的值,能够例如以下代码所看到的

NotificationCompat.WearableExtender wearableExtender =
new NotificationCompat.WearableExtender(notif);
boolean hintHideIcon = wearableExtender.getHintHideIcon();

传送通知

当须要传递通知时,使用 NotificationManagerCompat API取代NotificationManager:

// Get an instance of the NotificationManager service
NotificationManagerCompat notificationManager =
NotificationManagerCompat.from(mContext); // Issue the notification with notification manager.
notificationManager.notify(notificationId, notif);

当使用NotificationManager,一些 NotificationCompat.WearableExtender 的特性不能工作

NotificationCompat.WearableExtender wearableExtender =
new NotificationCompat.WearableExtender(notif);
boolean hintHideIcon = wearableExtender.getHintHideIcon();

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

  1. Android Wear 开发入门

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

  2. Android Wear开发 - 入门指引 - Eclipse开发平台搭建

    开发平台配置 下载最新版本的ADT,详情见官网:http://developer.android.com/sdk/installing/installing-adt.html .(之前一直习惯于Goo ...

  3. android wear开发之:创建可穿戴设备应用 - Creating Wearable Apps

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

  4. android wear开发:为可穿戴设备创建一个通知 - Creating a Notification for Wearables

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

  5. 一看就懂的Android APP开发入门教程

    一看就懂的Android APP开发入门教程 作者: 字体:[增加 减小] 类型:转载   这篇文章主要介绍了Android APP开发入门教程,从SDK下载.开发环境搭建.代码编写.APP打包等步骤 ...

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

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

  7. SpringMVC基础入门,创建一个HelloWorld程序

    ref:http://www.admin10000.com/document/6436.html 一.SpringMVC基础入门,创建一个HelloWorld程序 1.首先,导入SpringMVC需要 ...

  8. CXF 入门:创建一个基于WS-Security标准的安全验证(CXF回调函数使用,)

    http://jyao.iteye.com/blog/1346547 注意:以下客户端调用代码中获取服务端ws实例,都是通过CXF 入门: 远程接口调用方式实现 直入正题! 以下是服务端配置 ==== ...

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

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

随机推荐

  1. 简单的虚拟摇杆控制移动(NGUI)

    一.用NGUI创建虚拟摇杆贴图 先创建一个sprite作为背景叫做JoyStick 并添加一个BoxCollider,再创建一个sprite child作为虚拟摇杆中间的按钮,叫做button 二.通 ...

  2. OpenStack路: OpenStack建筑设计指南 - 概要(摘录和翻译)

    OpenStack它是在云技术领先的黄金工艺,作为一个组织,使各类企业,具有较大的灵活性和速度被发现,向市场推出自助服务云计算和基础架构即服务(IaaS)积.然,为了能够真正享受到这些好处,云计算必须 ...

  3. MySQL列:innodb的源代码的分析的基础数据结构

    在过去的一年中的数据库相关的源代码分析.前段时间分析levelDB实施和BeansDB实现,数据库网络分析这两篇文章非常多.他们也比较深比较分析,所以没有必要重复很多劳力.MYSQL,当然主要还是数据 ...

  4. android 联系数据库

    联系人数据库学习 2011-10-31(这是android2.3在接触db) 简单介绍 Android中联系人的信息都是存储在一个叫contacts2.db的数据库中.该数据库的路径是:/data/d ...

  5. 4.帧循环(游戏循环),schedule

     1 概述 游戏乃至图形界面的本质是不断地画图,然而画图并非任意的,不论什么游戏都须要遵循一定的规则来呈现出来,这些规则就体现为游戏逻辑.游戏逻辑会控制游戏内容,使其依据用户输入和时间流逝而改变. ...

  6. 开玩笑html5(五岁以下儿童)---绕地球月球,地球绕太阳运动(canvas实现,同样可以移动哦)

    请珍惜劳动小编成果,这篇文章是原来小编,转载请注明出处. 速度的參数与真实速度有点差距.大家能够自行调整 <!DOCTYPE html> <html> <head> ...

  7. Windows Phone 8 应用内截图

    WriteableBitmap wb = new WriteableBitmap(this.LayoutRoot, new MatrixTransform()); //wb.Render(this.L ...

  8. Kinect开发笔记之二Kinect for Windows 2.0新功能

    这是本博客翻译文档的第一篇文章.笔者已经苦逼的竭尽全力的在翻译了.但无奈英语水平也是非常有限.不正确或者不妥当不准确的地方必定会有,还恳请大家留言或者邮件我以批评指正.我会虚心接受. 谢谢大家.   ...

  9. 【MySQL笔记】mysql来源安装/配置步骤和支持中国gbk/gb2312编码配置

    不久的学习笔记.分享.我想有很大的帮助谁刚开始学习其他人的 备注:该票据于mysql-5.1.73版本号例如 1. mysql源代码编译/安装步骤 1) 官网下载mysql源代码并解压 2) cd至源 ...

  10. Docker contanier comunication with route

    2台宿主机,hslave1 192.168.1.153, hslave2 192.168.1.154 修改网段 docker0默认网段是172.17.0.0/16,修改154机器的docker0的网段 ...