使用Notification.Builder隐藏通知中的自动收报机文本

时间:2012-06-26 21:06:11

标签: android notifications ticker

我正在尝试显示正在进行的通知而没有初始的自动收报机文本显示。通过在构造函数中将ticker text设置为null,我能够使用旧样式通知:

mNotification = new Notification(R.drawable.ic_stat_playing, null, System.currentTimeMillis());

但是,我注意到现在不推荐以这种方式实例化通知,而是建议使用Notification.Builder。但是,即使我将滚动条文本设置为null,我也无法在没有滚动条文本的情况下显示通知:

Notification.Builder builder = new Notification.Builder(this);

CharSequence contentText = "contentText here";

Intent launchIntent = new Intent(this, MainActivity.class);

// The PendingIntent to launch our activity if the user selects this
// notification
PendingIntent contentIntent = PendingIntent.getActivity(this, -1,
                launchIntent, PendingIntent.FLAG_UPDATE_CURRENT);

builder.setContentIntent(contentIntent)
    .setSmallIcon(R.drawable.ic_stat_playing)
    .setLargeIcon(null)
    .setTicker(null)
    .setOnlyAlertOnce(true)                 
    .setWhen(System.currentTimeMillis())
    .setContentTitle(contentTitle)
    .setOngoing(true)
    .setContentText(contentText);

mNotification = builder.getNotification();

startForeground(NOTIFICATION_ID, mNotification);

是否无法使用新的Notification.Builder关闭自动收报机显示屏?是这样,这很不幸,因为我无法从弃用的代码中更新。

编辑 - 最终有效的代码:

mNotification = builder.getNotification();

mNotification.tickerView = null;

startForeground(NOTIFICATION_ID, mNotification);

2 个答案:

答案 0 :(得分:3)

您根本不需要包含.setTicker,只需将其留下:

builder.setContentIntent(contentIntent)
.setSmallIcon(R.drawable.ic_stat_playing)
.setLargeIcon(null)
.setOnlyAlertOnce(true)                 
.setWhen(System.currentTimeMillis())
.setContentTitle(contentTitle)
.setOngoing(true)
.setContentText(contentText);

答案 1 :(得分:2)

尝试在构建器之后将tickerView设置为null。这对我来说很好 代码如:

Notification notif = builder.build();
notif.tickerView = null;
mNotificationManager.notify(id, notif);