Android Wear:自定义通知 [英] Android Wear : Custom Notifications

查看:373
本文介绍了Android Wear:自定义通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在手持设备,可以通过使用自定义显示通知 RemoteViews 。 RemoteViews允许开发人员完全自定义通知。

什么是做Android Wear同样的方式吗?哪个类才可以使用自己定制的一个覆盖默认UI通知?


解决方案

  1. 如果你希望只自定义文本,您可以使用SpannableString。它允许你改变颜色,背景,调整您的标题/内容的文字。


  2. 如果你想创建完全不同的通知,你必须实现你穿的项目相似不便

     意图notificationIntent =新意图(背景下,WearNotificationActivity.class);
    的PendingIntent pendingNotificationIntent =
            PendingIntent.getActivity(上下文,0,notificationIntent,PendingIntent.FLAG_UPDATE_CURRENT);    通知通知=
                新Notification.Builder(上下文)
                        .setSmallIcon(R.drawable.ic_launcher)                   // .setContentTitle(CustomNotification)
                        .extend(新Notification.WearableExtender()
                                .setDisplayIntent(pendingNotificationIntent)
                                .setCustomSize preSET(Notification.WearableExtender.SIZE_LARGE)
                                .setStartScrollBottom(假)
                                .setHintHideIcon(真))                    。建立();    NotificationManager notificationManager =
                (NotificationManager)context.getSystemService(context.NOTIFICATION_SERVICE);    notificationManager.notify(0,通知);


在这里WearNotificationActivity - 你的活动容器自定义视图

请注意:你必须使用 .setSmallIcon(R.drawable.ic_launcher),即使你不需要它。
似乎是一个谷歌的错误,但没有这条线通知将不会被显示。

和设置

 的android:allowEmbedded =真
 机器人:taskAffinity =

针对活动容器

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.

  2. 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(R.drawable.ic_launcher) 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

这篇关于Android Wear:自定义通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆