为可穿戴设备创建通知

为android wear创建通知

NotificationCompat.Builder创建一个可以在手机上发送到可android wear通知.当用这个类创建通知时,系统负责在合适的时候在手机或者wear上展示这个通知

注意:对于使用RemoteViews来创建布局的通知在wear上仅仅显示文本和图标.但是你可以使用自定义的卡片布局创建自定的通知应用运行在wear上.

导入必须的类

build.gradle文件中添加下面这个依赖添加必须的引用包

compile "com.android.support:support-v4:20.0.+"

在导入必要的依赖库之后就有权限访问一些包,可以导入以下类

import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationManagerCompat;
import android.support.v4.app.NotificationCompat.WearableExtender;

使用Notification Builder创建通知

上面导入的v4库允许你创建一个有新特性(操作按钮,大图标)的通知,同时兼容android1.6及更高版本.

创建一个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());

当通知到达手机时,用户可以通过点击通知获取通过setContentIntent()该方法设置的PendingIntent.当在wear上时通过向左滑动通知,来打开这个PendingIntent.

添加操作按钮

除了通过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);

在手机上,这个操作像是一个附加在通知上的附加按钮.在wear上当用户向左滑动通知时这个操作像是一个大按钮.当用户触碰这个操作,此时这个关联的PendingIntent会显示.

提示:如果你的通知包含一个回复操作(比如消息应用),你可以通过wear的语音输入回复来增强行为.更多信息操作这里

wear上不一样的操作

如果你想wear上显示的行为操作和手机上的不一样,那么可以使用WearableExtender.addaction()这个方法.一旦使用了这个方法,在wear上不会在显示通过NotificationCompat.Builder.addAction()添加的行为操作.也就是说只有通过WearableExtender.addaction()添加的行为操作会显示在wear上不会显示在手机上.

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

添加一个大视图

通过向通知中添加一个大视图样式,可以在通知中插入扩展的文字内容.在手机上用户可以通过扩展的通知看到这个内容.在wear上这个大视图内容默认时显示的.

通过NotificationCompat.Builder的对象调用setStyle()方法添加扩展的内容.该方法可以传递BigTextStyle或者InboxStyle的对象实例

举个例子,下面的代码向通知中添加一个 NotificationCompat.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(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);

注意你可以使用setLargeIcon方法添加一个大图片到任何一个通知中,然而这些图标以大背景图展示在wear上而且不太好看因为这些图片被放大适应wear的屏幕尺寸.添加一个特殊的图片到通知上可以参考Add Wearable Features For a Notification,更多关于设计大图标的通知可以看这里

为通知添加可穿戴的特征

如果你想为你的通知添加可穿戴设备的特征,比如指定其他内容页面或者让用户通过语音输入指定文字回复.你可以使用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)
        .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();

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 the setContentIcon() method, in the res/drawable-hdpi directory.

这点提示没有看懂,好像是说对于400*400分辨率的图不能滚动,640*400的可以滚动.

如果你以后需要使用可穿戴特性特定选项,你可以使用特定属性的get方法.下面这个例子调用getHintHideIcon()方法获取是否隐藏了通知的图标.

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

Deliver the Notification (交付通知)

当希望交付传递一个通知时,需要用NotificationManagerCompat代替NotificationManager

// Get an instance of the NotificationManager service
NotificationManagerCompat notificationManager =
        NotificationManagerCompat.from(mContext);

// Issue the notification with notification manager.
notificationManager.notify(notificationId, notif);

如果使用framework层的NotificationManager,那么一些NotificationCompat.WearableExtender的特性不能使用.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值