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.

0 0
原创粉丝点击