如何在android中显示多行通知?

时间:2014-04-24 03:22:53

标签: android

我用多行做一个通知,代码如下:

private void sendNotification(String msg) {
    mNotificationManager = (NotificationManager)
            this.getSystemService(Context.NOTIFICATION_SERVICE);

    PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
            new Intent(this, Home.class), 0);

    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this)
    .setSmallIcon(R.drawable.ic_launcher).setContentTitle("");
    //
    // Set Big View style to see full content of the message
    NotificationCompat.BigTextStyle inboxStyle =
            new NotificationCompat.BigTextStyle();

    inboxStyle.setBuilder(mBuilder);
    inboxStyle.bigText(msg);
    inboxStyle.setBigContentTitle("");
    inboxStyle.setSummaryText("");
    // Moves the big view style object into the notification object.
    mBuilder.setStyle(inboxStyle);
    mBuilder.setContentText(msg);
    mBuilder.setDefaults(Notification.DEFAULT_ALL);
    mBuilder.setContentIntent(contentIntent);
    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build()); 
}

但它只显示一行通知,我希望它显示多行通知的所有内容。你能救我吗?

非常感谢你

4 个答案:

答案 0 :(得分:2)

大视图有三种风格:大图片风格,大文字风格,收件箱风格。下面的代码演示了BigTextStyle()的用法,它允许使用最多256 dp。

String longText = "...";

Notification noti = new Notification.Builder(this).
.....
.setStyle(new Notification.BigTextStyle().bigText(longText)) 

有关详情,请查看此reference link

答案 1 :(得分:1)

是的,可以做到。只需使用Big View进行通知即可。请参阅official documentation

这是一个很好的tutorial by vogella

答案 2 :(得分:1)

尝试添加SetStyleSetPriorityas,如下所示:

  .SetStyle(new Notification.BigTextStyle().BigText(message.Long_Message))   
  .SetPriority((int)NotificationPriority.Max)

它对我来说很好用:))

答案 3 :(得分:0)