简体   繁体   English

Android状态栏多行Notification API 14

[英]Android Status bar multiline Notification API 14

I'm using API 14 device. 我正在使用API​​ 14设备。 I have been trying to develop the multiline notification similar to Gmail notifications. 我一直在尝试开发类似于Gmail通知的多行通知。 I have gone through several stack overflow questions but didn't find any solution which would give me multiline notification for API<16. 我经历了几个堆栈溢出问题,但是没有找到任何解决方案,该解决方案会给我API <16的多行通知。 Please note that Big view notification works only for OS 4.1+ and I want this for API 14. 请注意,“大视图”通知仅适用于OS 4.1+及更高版本,我希望将其用于API 14。

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
    NotificationCompat.InboxStyle inboxStyle;
    inboxStyle = new NotificationCompat.InboxStyle();

    inboxStyle.addLine("Hello 1")
    .addLine("Hello 2")
    .addLine("Hello 3")
    .addLine("Hello 4");

    builder.setStyle(inboxStyle);
    builder.setTicker("HELLO WORLD MSG");
    builder.setSmallIcon(android.R.drawable.sym_def_app_icon);
    builder.setContentTitle("HELLO WORLD");
    builder.setPriority(NotificationCompat.PRIORITY_HIGH);
    notificationManager.notify(1000, builder.build());

这是我获得的结果。这里的字符串“ hello 1”,“ hello2”,“ hell3”等不可见

I am not getting Strings "hello 1", "hello 2", ... in separate lines. 我没有在单独的行中得到字符串“ hello 1”,“ hello 2”,...。 Please Help me... 请帮我...

EDIT: Since many wrong solutions have been posted, I would like to point out that the multiline notifications are working fine in other modern mobiles(nexus). 编辑:由于已经发布了许多错误的解决方案,我想指出的是,多行通知在其他现代手机(nexus)中也能正常工作。 The code I posted is not faulty/buggy. 我发布的代码不是错误/越野车。 But it doesn't work for API 14. 但这不适用于API 14。

Try Following code 尝试以下代码

String[] names=new String[5];
        names[0]="Hello 1";
        names[1]="Hello 2";
        names[2]="Hello 3";
        names[3]="Hello 4";
        names[4]="Hello 5";
        inboxStyle.setBigContentTitle("Hello");
        for (String name:names)
        {
            inboxStyle.addLine(name);
        }

        builder.setStyle(inboxStyle);

OR, you can also refer this 或者,您也可以参考

Firstly I will not recommend API14 (as it is obsolete), but if you still want to have multiple line notification,you can have by using Notification.InboxStyle 首先,我不建议您使用API​​14(因为它已过时),但是如果您仍然希望有多行通知,可以使用Notification.InboxStyle

Notification.InboxStyle inboxStyle = new Notification.InboxStyle();     
String[] events = new String[]{"h1","h2","h3","h4"};                //your lines
inboxStyle.setBigContentTitle("Event tracker details:");
for(int i=0; i < events.length; i++) {
   inboxStyle.addLine(events[i]);
}
Notification notification = new Notification.Builder(this)
        .setSmallIcon(R.drawable.ic_launcher)
        .setContentIntent(pintent)
        .setStyle(inboxStyle)           
        .build();

Reference 参考

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM