安卓通知大图标,有没有办法删除右下角的小图标?

15

我有一个通知,其中显示了一个大图标。

有没有办法在蜂巢及以上版本的设备上从此视图中删除更小的图标?

显然仍然保留顶部状态栏的小图标。

enter image description here

   NotificationCompat.Builder builder = new NotificationCompat.Builder(context)


            // Set required fields, including the small icon, the
            // notification title, and text.
            .setSmallIcon(R.drawable.ic_notify_status_new)
            .setContentTitle(title)
            .setContentText(text)


            // All fields below this line are optional.

            // Use a default priority (recognized on devices running Android
            // 4.1 or later)
            .setPriority(NotificationCompat.PRIORITY_DEFAULT)

            // Provide a large icon, shown with the notification in the
            // notification drawer on devices running Android 3.0 or later.
            .setLargeIcon(picture)

            .setContentIntent(
                    PendingIntent.getActivity(
                            context,
                            0,
                            notiIntent,
                            PendingIntent.FLAG_UPDATE_CURRENT)
            );


    builder = getNotiSettings(builder);
    notify(context, builder.build());

大视图是在Android 4.1中引入的,它们不支持旧设备。http://developer.android.com/training/notify-user/expanded.html - Phantômaxx
你解决了这个问题吗? - kb_14
3个回答

14

在构建通知后添加此代码。

   if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        int smallIconViewId = getResources().getIdentifier("right_icon", "id", android.R.class.getPackage().getName());

        if (smallIconViewId != 0) {
            if (notif.contentIntent != null)
                notif.contentView.setViewVisibility(smallIconViewId, View.INVISIBLE);

            if (notif.headsUpContentView != null)
                notif.headsUpContentView.setViewVisibility(smallIconViewId, View.INVISIBLE);

            if (notif.bigContentView != null)
                notif.bigContentView.setViewVisibility(smallIconViewId, View.INVISIBLE);
        }
    }

其中“notif”是您构建的通知对象。


非常感谢,这对我有用,我没有检查SDK_INT,只需获取smallIconViewId并将视图设置为不可见即可在低于21的API上正常工作。 - ShahinFasihi
你的意思是 if (notif.contentView != null) 而不是 notif.contentIntent 吗? - Minas Mina
没有,这是在点击扩展状态条目时执行的意图。 - Ziv Kesten
notif.contentIntent和notif.contentView之间的关系是什么? - Ranmocy
1
请将 if (notif.contentIntent != null) 更改为 if (notif.contentView != null) - almisoft
3
Notification.contentView.setImageViewResource() 是可行的,但 contentView 已经被弃用。使用 NotificationCompat.Builder.getContentView().setImageViewResource()(在 NotificationCompat.Builder.build()NotificationManager.notify() 之前或之后)是不起作用的。 - almisoft

1

是的,它是重复的。 不,还有另一种方法:不使用“setLargeIcon”。 - Darek Deoniziak

-1

只使用setSmallIcon设置您的图标。如果未指定第二个图标,它也将用于largeIcon(请确保它足够大以适应两者)。

确保它在pre-lolipop,lolipop和以上版本上看起来很好。例如,在此处,我仅为pre-lolipop设备设置应用程序图标,其中lolipop及以上版本会获得不同的smallIcon和与smallIcon相同的largeIcon。

int currentApiVersion = android.os.Build.VERSION.SDK_INT;
if (currentApiVersion >= Build.VERSION_CODES.LOLLIPOP) {
    notificationBuilder
        .setSmallIcon(smallIcon)
        .setLargeIcon(appIconDrawable)
} else {
    notificationBuilder.setSmallIcon(appIconDrawable);
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接