Android 為 Notification 添加頁面

2018-08-02 17:48 更新

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

當(dāng)開發(fā)者想要在不需要用戶在他們的手機(jī)上打開app的情況下,還可以允許表達(dá)更多的信息,那么開發(fā)者可以在可穿戴設(shè)備上的Notification中添加一個(gè)或多個(gè)的頁面。添加的頁面會馬上出現(xiàn)在主 Notification 卡片的右邊。

 

為了創(chuàng)建一個(gè)擁有多個(gè)頁面的 Notification,開發(fā)者需要:

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

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

// 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


以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號