Why doesn't Jelly Bean show the second row in a Notification?

[亡魂溺海] 提交于 2019-12-23 07:49:39

问题


I'm currently looking into the NotificationCompat features of the Android Support Package v4 Rev 10. The documentation says that 'setContentText()' shows the second row in a notification. This is true for API 8 up to API 15. However, if I try to use this method in API 16 my Notifications will miss the second line. I see only the Title but not the second line. Adding multiple lines is no problem (using 'addline()').

Here's my code for the NotificationCompat.Builder I used:

private NotificationCompat.Builder buildNormal(CharSequence pTitle) {
    NotificationCompat.Builder builder = new NotificationCompat.Builder(
            getSherlockActivity());

    builder.setAutoCancel(true).setDefaults(Notification.DEFAULT_ALL);
    // set the shown date
    builder.setWhen(System.currentTimeMillis());
    // the title of the notification
    builder.setContentTitle(pTitle);
    // set the text for pre API 16 devices
    builder.setContentText(pTitle);
    // set the action for clicking the notification
    builder.setContentIntent(buildPendingIntent(Settings.ACTION_SECURITY_SETTINGS));
    // set the notifications icon
    builder.setSmallIcon(android.R.drawable.stat_sys_download_done);
    // set the small ticker text which runs in the tray for a few seconds
    builder.setTicker("This is your ticker text.");
    // set the priority for API 16 devices
    builder.setPriority(Notification.PRIORITY_DEFAULT);
    return builder;
}

And if I want to add multiple lines and show the notification, I uses this code:

NotificationCompat.Builder normal = buildNormal("This is an Expanded Layout Notification.");
    NotificationCompat.InboxStyle big = new NotificationCompat.InboxStyle(
            normal);

    // summary is below the action
    big.setSummaryText("this is the summary text");
    // Lines are above the action and below the title
    big.addLine("This is the first line").addLine("The second line")
            .addLine("The third line").addLine("The fourth line");

    NotificationManager manager = getNotificationManager(normal);
    manager.notify(Constants.NOTIFY_ID, big.build());

Is this a wanted feature of Jelly Bean, that setContentText is ignored or am I missing something? The code runs on all versions without errors, but I would like to add the second line with the same code I use on ICS or earlier.

I've also added two screenshots. the first from my ICS 4.0.3 Huawei MediaPad and the second from a Galaxy Nexus with 4.1.1. The second line from 1 is for simplicity the same String as the notification title. It is not visible on 2.

Thanks for your help in advance!


回答1:


Is this a wanted feature of Jelly Bean, that setContentText is ignored or am I missing something?

The value of setContextText() should be visible in the collapsed state (e.g., two-finger swipe up if expanded, or have it not be the top-most Notification). It will be replaced by NotificationCompat.InboxStyle in the expanded state, given your code above.




回答2:


If you want to show the notification by default expanded state.Simply set PRIORITY_MAX of builder. Like as: builder.setPriority(Notification.PRIORITY_MAX);




回答3:


I've fixed this problem now with @CommonsWare's help and created a simple method, that will check the current API Level and decides what command should be used:

private void createCompatibleSecondLine(CharSequence pTitle,
        NotificationCompat.Builder pBuilder, InboxStyle pInboxStyle) {
    // set the text for pre API 16 devices (or for expanded)
    if (android.os.Build.VERSION.SDK_INT < 16) {
        pBuilder.setContentText(pTitle);
    } else {
        pInboxStyle.setSummaryText(pTitle);
    }
}

So it's nothing wild and far from perfect but it does the job for me. Improvements are always welcome :)



来源:https://stackoverflow.com/questions/11988073/why-doesnt-jelly-bean-show-the-second-row-in-a-notification

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!