android Wearable-Adding Wearable Features to Notifications
来源:互联网 发布:淘宝卖灵符 法律 编辑:程序博客网 时间:2024/04/25 23:53
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 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 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.
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.Builder
and issue the notification by passing it to notify()
. For example:
int notificationId = 001;// Build intent for notification contentIntent 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 serviceNotificationManagerCompat 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 mapIntent 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 withWearableExtender.addAction()
appear on the wearable and they do not appear on the handheld.
// Create an intent for the reply actionIntent actionIntent = new Intent(this, ActionActivity.class);PendingIntent actionPendingIntent = PendingIntent.getActivity(this, 0, actionIntent, PendingIntent.FLAG_UPDATE_CURRENT);// Create the actionNotificationCompat.Action action = new NotificationCompat.Action.Builder(R.drawable.ic_action, getString(R.string.label), actionPendingIntent) .build();// Build the notification and add the action via WearableExtenderNotification 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 wearablesNotificationCompat.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 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. > 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 actionIntent 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 inputNotificationCompat.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 WearableExtenderNotification 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 notificationNotificationManagerCompat 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 asClipData
. The getResultsFromIntent()
method provides a convenient way to receive a character sequence without having to process the ClipData
yourself. - android Wearable-Adding Wearable Features to Notifications
- Android Wear 进阶 1 Adding Wearable Features to Notifications 添加手表特性到通知上
- android Wearable-Adding Pages to a Notification and Stacking Notifications
- android wearable-Adding Voice Capabilities
- Android wearable
- android wearable初体验
- android wearable 研究
- Failed to Find Android Wearable Dependency的解决办法
- Android Wearable 工程创建、运行
- android wearable-Creating Custom Layouts
- android Wearable-Creating Wearable Apps and Creating Custom Layouts
- android Wearable-Packaging Wearable Apps and Debugging over Bluetooth
- Wearable ‘AirMouse’
- android wearable-Creating Cards and Creating Lists
- android Wearable-Sending and Syncing Data
- android Wearable-Accessing the Wearable Data Layer and Syncing Data Items
- Wearable computing system
- 如何打包Wearable Apps
- C++中四种类型转换方式
- (2-4)HDFS的Java接口,上传,下载,创建文件,删除文件
- 一般情况下的实体设计,Struts2,String,hibernate快速准确的从零开始实现增删改查功能
- bzoj2395: [Balkan 2011]Timeismoney
- R绘图中使用中文字体
- android Wearable-Adding Wearable Features to Notifications
- DevExpress 中根据数据库字典动态生成卡式菜单
- View.onMeasured的默认实现 (onMeasure必须调setMeasuredDimension)
- 牛客网 | 魔术索引II
- 简单的数据查询
- C++中vector向量的用法(自整)
- android listview 连续调用 getview问题分析及解决。
- 用opengl方式打开unity
- DPM(Deformable Parts Model)原理及代码分析