Android Wear 进阶 1 Adding Wearable Features to Notifications 添加手表特性到通知上

Adding Wearable Features to Notifications

添加手表特性到通知上面

Dependencies and prerequisites

You should also read

When an Android handheld (phone or tablet) and Android wearable are connected, the handheld automatically shares notifications with the wearable. On the wearable, each notification appears as a new card in the context stream.

However, to give users the best experience, you should add wearable-specific functionality to the notifications you already create. The following lessons show you how to create notifications that are catered for handhelds and wearables at the same time.

<翻译>当一个手机端和手表连接后。手表会自动分享通知到手表端。在手表上面,每一个个通知将会以新的卡片card的形式在上下文流中,(其实就是手表的界面上面)。

然而,为了给用户更好的体验,你应该增加手表特殊的功能到你已经创建了的通知上面。下面的课程展示你怎么去一次创建手表端和手机端不同目录的通知的。



Figure 1. The same notification displayed on a handheld and wearable.

图表1:同样的通知战士在手表端和手机端。

Lessons课程



Creating a notification 创建一个通知
Learn how to create notifications with wearable features with the Android support library.
学习如何通过Android 支持包 support library 来创建有手表特性的通知
Receiving Voice Input in a Notification 接收通知中的语言输入
Learn how to add an action to a wearable notification that receives voice input from users and delivers the transcribed message to your handset app.
学习如何去给手表通知添加一个action。这个action 可以用来接收用户的声音输入并且将可以翻译了信息传给你的手机应用。
Adding Pages to a Notification添加页面到通知
Learn how to add additional pages of information that are visible when the user swipes to the left.
学习如何添加信息页面,这个信息页面在用户从左滑动通知的时候可见。
Stacking Notifications 堆积通知
Learn how to place all similar notifications from your app in a stack, allowing users to view each notification individually without adding multiple cards to the card stream.
学习如何去将类似的通知放子啊一个堆里面,这个允许用户去查看每一个通知而不需要多个卡片在卡片流里面。


1 Creating a Notification for Wearables

1,创建手表的通知
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

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 notificationsthat use custom card layouts by creating a wearable app that runs on the wearable device.

<翻译>注意通知使用RemoteViews的话,会出去自定义的布局,并且手表端只显示文字和icons。然后你可以创建“自定义的通知” 这个自定义的通知可以自定义卡片布局,它通过创建一个运行在手表设备上面的手表应用。

Import the necessary classes导入必须的类


To import the necessary packages, add this line to yourbuild.gradlefile:

为了导入必须的类,将下面的话添加到你的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;

Create Notifications with the Notification Builder

通过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支持包允许你去创建通知,这个通知里面可以使用通知的特性例如action 的按钮和大的icon,可以和 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 thesetContentIntent() 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.

<翻译>当这个通知出现在了手机设备上,用户可以通过点击通知来触发PendingIntent,这个PendingIntent 通过setContentIntent()来指定。

当这个通知出现在手表上面,用户可以通过从左向右滑动通知去显示打开的action,这个action 会出发在手机设备上面的actionl。

Add Action Buttons 添加action 的按钮


In addition to the primary content action defined bysetContentIntent(), you can add other actions by passing aPendingIntent to the addAction() method.

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.

<翻译>除了通过setContentIntentn()方法来定义的主要的内容,你还可以通过给PendingIntent 传送一个addAction()的方法来添加其他的额action。

例如在下面的代码中显示了和上面相同的通知类型,但是添加了一个action 去在地图上查看事件发生的地理位置。

// 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.

在手机端,一个action在通知上面显示的是一个增加的按钮。在手表端,显示的是一个大的按钮,这个按钮在用户从左向右的滑动的时候显示。

当用户点击了这个action,相关的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.

提示:如果你的通知包含了Reply回复的action,例如信息的app上面就有,那么你可以扩展这个行为通过使用android 手表的voice input 声音输入回复。更多的信息,可以查看 Receiving Voice Input from a Notification.

Specify Wearable-onlyActions 制定只是手表段的action


If you want the actions available on the wearable to be different from those on the handheld, then useWearableExtender.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 withWearableExtender.addAction() appear on the wearable and they do not appear on the handheld.

如果你想要action在手表端和在手机端不同,那么请使用WearableExtender.addAction(). 一旦你通过这个方法添加了action,这个手表将不会展示任何其他的通过 NotificationCompat.Builder.addAction().来添加的action 。也就是说,被WearableExtender.addAction() 添加的action只会出现在手表端而不会出现在手机端。

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

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.

你通过添加一个“大图big view”样式到你的通知来插入扩展的消息内容到你的通知。在手机设备上面,用户可以看到大的视图内容通过扩展通知。在手表段,大图的内容直接就是默认的可见的。

To add the extended content to your notification, call setStyle() on theNotificationCompat.Builder object, passing it an instance of eitherBigTextStyle or InboxStyle.

为了添加扩展的内容到你的通知,调用NotificationCompat.Builder 的setStyle()的方法,传送一个要不是BigTextStyle要不是InboxStyle的实例。

For example, the following code adds an instance ofNotificationCompat.BigTextStyle to the event notification, in order to include the complete event description (which includes more text than can fit into the space provided forsetContentText()).

例如,下面的代码添加了NotificationCompat.BigTextStyle的实例到事件的通知,为了包含了事件描述,这个事件描述包含了比setConetentText 的内容更多的内容。

// 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()的方法来添加一个大的icon图片。然而这些icons出现在手表上面显示的时候是作为大的背景图片的,并且这个图片因为没有适配手表屏幕所以看上去不是很好。为了增加特别的手表背景图片到通知,看Add Wearable Features For a Notification. 更多的关于使用大图片设计通知,请看 Design Principles of Android Wear.

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.WearableExtenderclass to specify the options. To use this 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.

For example, the following code calls the setHintHideIcon() method to remove the app icon from the notification card.

如果你需要添加特别的手表项目到一个通知,例如制定增加有内容的page页面或者让用户听写语音输入一个文字回复,你可以使用 NotificationCompat.WearableExtender类来制定这些选项。为了使用这个API:

1:创建一个WearableExtender的实例,设置手表特性的选项给通知

2:创建 NotificationCompat.Builder的实例,设置 一些需要的属性

3:调用通知的的extend()的方法,并且传递到WearableExtender里面。这个应用手表选项到通知上面。

4:调用build()去创建通知

例如下面的代码调用过了setHintHideIcon()方法去从通知的卡片上面一处应用icon

// 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 theres/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.

注意:你使用setBackground()方法设置的背景图片应该使用400*400的给不是滑动的背景,640*400给支持水平滑动滑动的背景。将这些图片放在 drawable-nodpi  目录下面,将其他的不是手表通知的非图片资源防止在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 callsthe getHintHideIcon() method to get whether or not this notification hides the icon:

如果你之后需要读取手表特别的选项,使用相关的选项的get方法。这里例子调用了getHitHideIcon()方法去得到是否这个通知隐藏了icon了。

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

Deliver the Notification 发送通知


When you want to deliver your notifications, always use the NotificationManagerCompat API instead ofNotificationManager:

当你想要发送 你的额通知,一般是使用 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);

If you use the framework's NotificationManager, some features from NotificationCompat.WearableExtenderdo not work, so make sure to use NotificationCompat. (should be NotificationManagerCompat)

如果你使用了框架的 NotificationManager, 一些 NotificationCompat.WearableExtender中的特性就不能使用了,所以要保证使用NotificationManagerCompat

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值