返回介绍

為Notification添加顯示頁面

发布于 2020-04-01 13:16:45 字数 2776 浏览 1092 评论 0 收藏 0

編寫:wangyachen - 原文:http://developer.android.com/training/wearables/notifications/pages.html

當開發者想要在不需要用戶在他們的手機上打開app的情況下,還可以允許表達更多的信息,那麼開發者可以在可穿戴設備上的Notification中添加一個或多個的頁面。添加的頁面會馬上出現在主 Notification 卡片的右邊。

為了創建一個擁有多個頁面的 Notification,開發者需要:

  1. 通過NotificationCompat.Builder創建主Notification(首頁),以開發者想要的方式使其出現在手持設備上。
  2. 通過NotificationCompat.Builder為可穿戴設備添加更多的頁面。
  3. 通過addPage()方法將這些頁面應用到主 Notification 中,或者通過addPages()將多個頁面添加到一個Collection

舉個例子,以下代碼為Notification添加了第二個頁面:

// Create builder for the main notification
NotificationCompat.Builder notificationBuilder =
        new NotificationCompat.Builder(this)
        .setSmallIcon(R.drawable.new_message)
        .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();

// Add second page with wearable extender and extend the main notification
Notification twoPageNotification =
        new WearableExtender()
                .addPage(secondPageNotification)
                .extend(notificationBuilder)
                .build();

// Issue the notification
notificationManager =
        NotificationManagerCompat.from(this);
notificationManager.notify(notificationId, twoPageNotification);

下一課:以Stack的方式顯示Notifications

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文