【问题标题】:I created pages in notification, the code shows no error but in wear, not showing me any pages我在通知中创建了页面,代码显示没有错误但磨损了,没有显示任何页面
【发布时间】:2014-03-22 10:37:10
【问题描述】:

我在通知中创建了页面,代码显示没有错误但磨损了,没有显示任何页面....这是代码:

    int notificationId = 001;
    // Build intent for notification content
    Intent viewIntent = new Intent(this, MainActivity.class); 
    viewIntent.putExtra("Helllo", "Hello Wear !");
    PendingIntent viewPendingIntent =
            PendingIntent.getActivity(this, 0, viewIntent, 0);

    NotificationCompat.Builder notificationBuilder =
            new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_launcher) 
            .setContentTitle("My App") 
            .setContentText("Hello Wear !")
            .setContentIntent(viewPendingIntent);

    // Get an instance of the NotificationManager service
    NotificationManagerCompat notificationManager =
            NotificationManagerCompat.from(this);

    // Build the notification and issues it with notification manager.
    notificationManager.notify(notificationId, notificationBuilder.build());
    if (savedInstanceState == null) { 
        getFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment()).commit();


            // Create builder for the main notification
        NotificationCompat.Builder notificationCompatBuilder =
                new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.ic_launcher)
                .setContentTitle("Page 1")
                .setContentText("Short message")
                .setContentIntent(viewPendingIntent);

        // Create a big text style for the second page
        BigTextStyle secondPageStyle = new NotificationCompat.BigTextStyle();
        secondPageStyle.setBigContentTitle("Page 2")
                       .bigText("A lot of text...");

        // Create second page notification
        Notification secondPageNotification =
                new NotificationCompat.Builder(this)
                .setStyle(secondPageStyle)
                .build();

        // Create main notification and add the second page
        Notification twoPageNotification =
                new WearableNotifications.Builder(notificationBuilder)
                .addPage(secondPageNotification)
                .build();

代码现在显示错误并且运行良好,但只显示一页而不是两页。

【问题讨论】:

    标签: java android wear-os


    【解决方案1】:

    您收到的一页通知是否显示“我的应用”/“Hello Wear!”?

    这是合乎逻辑的,因为您正在构建此通知 (var notificationBuilder) 并实际显示它 (notificationManager.notify())。

    对于双页通知,您创建了第一页和第二页 - 但是在将第一页和第二页合并到 twoPageNotification 时,您在第一页使用 notificationBuilder 而不是 notificationCompatBuilder

    此外,构建的两页通知twoPageNotification 永远不会传递给notify() 函数。所以添加notificationManager.notify(notificationId,twoPageNotification) 应该会显示这两个页面。

    【讨论】:

    • 添加时出现错误:notificationManager.notify(notificationId,twoPageNotification),表示 twoPageNotification 有错误。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-05
    • 1970-01-01
    • 2015-07-18
    • 1970-01-01
    • 2016-12-10
    • 2014-01-20
    相关资源
    最近更新 更多