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

创建通知

为了创建在手机与可穿戴设备中都能展现的通知,可以使用 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()主要内容的行为你可以通过传递一个PendingIntentaddaction()方法添加其他行动

例如,下面的代码展示了与上面代码相同类型的通知,但是添加了一个动作在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是以一个大按钮形式出现的,当用户单击该按钮,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),,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。