问题
In handheld devices, custom notifications can be displayed using RemoteViews. RemoteViews allows the developer to fully customise the notification.
What is the way to do the same for Android Wear? Which class should be used to override the default notification UI with my own customised one?
回答1:
if you want to customize text only, you can use SpannableString. It allows you to change color, background, align of your title/content text.
if you want to create totally different notification you have to implement smth similar in your wear project
Intent notificationIntent = new Intent(context, WearNotificationActivity.class); PendingIntent pendingNotificationIntent = PendingIntent.getActivity(context, 0, notificationIntent,PendingIntent.FLAG_UPDATE_CURRENT); Notification notification = new Notification.Builder(context) .setSmallIcon(R.drawable.ic_launcher) // .setContentTitle("CustomNotification") .extend(new Notification.WearableExtender() .setDisplayIntent(pendingNotificationIntent) .setCustomSizePreset(Notification.WearableExtender.SIZE_LARGE) .setStartScrollBottom(false) .setHintHideIcon(true)) .build(); NotificationManager notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE); notificationManager.notify(0, notification);
where WearNotificationActivity - activity container of you custom view.
NOTE: you HAVE TO use .setSmallIcon(..)
even if you don't need it.
Seems like a google bug, but without this line notification won't be shown.
and set
android:allowEmbedded="true"
android:taskAffinity=""
for your activity container
回答2:
To create a rich Notification for Android Wear, you have to use NotificationCompat.Builder
from support-v4.
This version gives you a better control of the notification layout on Wear with methods such as .setActionButton()
or .setStyle()
.
You can even more customize your notification with a NotificationCompat.WearableExtender
.
Learn more at Creating a Notification
回答3:
Currently there is no way to do that. EDIT: it is possible: Custom UI for Android Wear Notifications
Best option out there are:
- Create Notification on phone and use
NotificationCompat.WearableExtender
notification for both phone and Wear. - Create Notification with
setLocalOnly()
so notification will be limited to phone, and on Wear create separate one - with other look, actions etc. - Do as above but instead of notification on Wear create own app with CardFrames (that allows you to have "notification style" and custom layout at same time, and when receiving signal from phone run your app instead of notification.
So only last option allows you to have custom layout but there are many drawback (as it is own app) - for example it is separate from notification list.
Hope that might change in the future.
来源:https://stackoverflow.com/questions/27014866/android-wear-custom-notifications