android Wearable-Adding Wearable Features to Notifications
2016-01-11 20:31
465 查看
When an Android handheld (phone or tablet) and Android wearable are connected, the handheld automatically shares notifications with the wearable.
To build handheld notifications that are also sent to wearables, use
When you build notifications with this class, the system takes care of displaying notifications properly, whether they appear on a handheld or wearable.
Note: Notifications
using
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.
Now that your project has access to the necessary packages, import the necessary classes from the support library:
> 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.
To create a notification with the support library, you create an instance of
issue the notification by passing it to
adds an action to view the event location on a map.
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.
That is, only the actions added with
on the wearable and they do not appear on the handheld.
Note: The bitmap that you use with
have a resolution of 400x400 for non-scrolling backgrounds and 640x400 for backgrounds that support parallax scrolling. Place these bitmap images in the
Place other non-bitmap resources for wearable notifications, such as those used with the
in the
> Note: The
Android emulator does not support voice input. When using the emulator for a wearable device, enableHardware keyboard present in
the AVD settings so you can type replies instead.
Note: Do not use
obtain the voice result, because the voice input is stored as
The
provides a convenient way to receive a character sequence without having to process the
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.
Note: Notifications
using
RemoteViewsare
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.
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;
> 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.
To create a notification with the support library, you create an instance of
NotificationCompat.Builderand
issue the notification by passing it to
notify(). For example:
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());
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);
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.
That is, only the actions added with
WearableExtender.addAction()appear
on the wearable and they do not appear on the handheld.
// 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();
// Create a WearableExtender to add functionality for wearables NotificationCompat.WearableExtender wearableExtender = new NotificationCompat.WearableExtender() .setHintHideIcon(true) .setBackground(mBitmap);
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-nodpidirectory.
Place other non-bitmap resources for wearable notifications, such as those used with the
setContentIcon()method,
in the
res/drawable-hdpidirectory.
> Note: The
Android emulator does not support voice input. When using the emulator for a wearable device, enableHardware keyboard present in
the AVD settings so you can type replies instead.
Add the Voice Input as a Notification Action
// Create an intent for the reply action Intent replyIntent = new Intent(this, ReplyActivity.class); PendingIntent replyPendingIntent = PendingIntent.getActivity(this, 0, replyIntent, PendingIntent.FLAG_UPDATE_CURRENT); // Create the reply action and add the remote input NotificationCompat.Action action = new NotificationCompat.Action.Builder(R.drawable.ic_reply_icon, getString(R.string.label), replyPendingIntent) .addRemoteInput(remoteInput) .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(); // Issue the notification NotificationManagerCompat notificationManager = NotificationManagerCompat.from(mContext); notificationManager.notify(notificationId, notification);
Note: Do not use
Intent.getExtras()to
obtain the voice result, because the voice input is stored as
ClipData.
The
getResultsFromIntent()method
provides a convenient way to receive a character sequence without having to process the
ClipDatayourself.
相关文章推荐
- Android 设置圆角虚线
- android性能优化
- android数据库sqlite
- Android回调事件传播-android学习之旅(四十五)
- Android悬浮窗实现 使用WindowManager
- android中fragment与activity之间通信原理以及例子
- Android 实现沉浸式状态栏
- Android中px、sp、dip的区别
- Android studio中"ADB not responding."之处理
- Android 动画效果
- Android项目总结
- Android 读取目录中的文件列表并展示
- android 自动拨打含“#”如:*1234#的电话
- Android Studio 调试过程中快捷查看断点处变量值(Ctrl+Shift+I无效)?
- Android开发环境搭建与配置
- Android 布局Layout 性能优化<8>
- Android获取点击屏幕的位置坐标
- android随笔21——SlidingMenu侧滑菜单
- Android 优化List图片显示
- 29.Android之文本框输入自动提示学习