【问题标题】:Android Status bar multiline Notification API 14Android状态栏多行通知API 14
【发布时间】:2016-02-24 09:42:19
【问题描述】:

我正在使用 API 14 设备。我一直在尝试开发类似于 Gmail 通知的多行通知。我经历了几个堆栈溢出问题,但没有找到任何解决方案可以为我提供 API请注意,大视图通知仅适用于 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”、“hello 2”、...。请帮帮我...

编辑:由于发布了许多错误的解决方案,我想指出多行通知在其他现代手机(nexus)中运行良好。我发布的代码没有错误/错误。但它不适用于 API 14。

【问题讨论】:

    标签: java android android-notifications android-statusbar


    【解决方案1】:

    试试下面的代码

    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);
    

    或者,您也可以参考this

    【讨论】:

    • 哥们请好好看看代码!!!我已经添加了 inboxStyle.addLine("Hello 1") .addLine("Hello 2") .addLine("Hello 3") .addLine("Hello 4");这或多或少是你想要做的。
    【解决方案2】:

    首先我不会推荐API14(因为它已经过时了),但是如果你仍然想要多行通知,你可以使用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

    【讨论】:

    • 伙计,您只是想在循环中添加字符串,而我正在使用构建器模式一一添加它们。 inboxStyle.addLine("Hello 1") .addLine("Hello 2") .addLine("Hello 3") .addLine("Hello 4");请再看一遍。这不是一个合适的解决方案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-15
    • 2016-02-22
    • 1970-01-01
    相关资源
    最近更新 更多