一 功能解析
- 氣泡內(nèi)置于“通知”系統(tǒng)中。它們浮動(dòng)在其他應(yīng)用內(nèi)容上層,并會(huì)跟隨用戶轉(zhuǎn)到任意位置呐籽。氣泡可以展開以顯示應(yīng)用功能和信息,并可在不使用時(shí)收起蚀瘸。
- 當(dāng)設(shè)備處于已鎖定狀態(tài)或“顯示屏始終保持開啟狀態(tài)”處于活動(dòng)狀態(tài)時(shí)狡蝶,氣泡就會(huì)像普通的通知那樣顯示。
二 作用分析
讓用戶的通知消息以一種更美觀的方式展示贮勃,氣泡旨在成為 SYSTEM_ALERT_WINDOW的替代方案贪惹; 當(dāng)APP有新的通知消息,APP的Icon上會(huì)自動(dòng)追加未讀消息提醒藍(lán)點(diǎn)(限androidR 的版本)寂嘉。
三 實(shí)現(xiàn)的偽代碼
(1) 前置條件
氣泡的展開視圖是根據(jù)您選擇的 Activity 創(chuàng)建的奏瞬。此 Activity 需要經(jīng)過(guò)配置才能正確顯示為氣泡。此 Activity 必須可以調(diào)整大小泉孩,支持文檔界面模式下啟動(dòng)硼端。如果不滿足其中任何要求,便會(huì)改為顯示為通知寓搬。
<activity
android:name=".bubbles.BubbleActivity"
android:theme="@style/AppTheme.NoActionBar"
android:label="@string/title_activity_bubble"
android:allowEmbedded="true"
android:documentLaunchMode="always"
android:resizeableActivity="true"
/>
(2) 參考實(shí)例實(shí)現(xiàn)代碼
// androidR 系統(tǒng) 發(fā)送氣泡通知樣式到 通知欄的功能
@RequiresApi(Build.VERSION_CODES.Q)
private fun androidR_bubbleNotification() {
val target = Intent(this, PressorTestActivity::class.java)
val contentIntent = PendingIntent.getActivity(this, 0, target, PendingIntent.FLAG_UPDATE_CURRENT)
// 氣泡窗口點(diǎn)擊之后珍昨,窗口點(diǎn)擊之后跳轉(zhuǎn)的activity 跟contentIntent 的目標(biāo)activity設(shè)置為不相同個(gè),
// 最后還是為已contentInten設(shè)置的目標(biāo)Intent為準(zhǔn)
val bubbleIntent = PendingIntent.getActivity(
this,
2020,
target,
PendingIntent.FLAG_UPDATE_CURRENT)
// create bubble metaData
val bubbleData = Notification.BubbleMetadata.Builder()
.setDesiredHeight(600)
.setIcon(Icon.createWithResource(this, R.mipmap.ic_launcher))
.setAutoExpandBubble(true)
.setSuppressNotification(true)
.setIntent(bubbleIntent)
.build()
// Create person object
val personA = Person.Builder()
.setBot(true)
.setName("userA")
.setIcon(Icon.createWithResource(this, R.mipmap.ic_launcher))
.setImportant(true)
.build()
val personB = Person.Builder()
.setBot(true)
.setName("userB")
.setIcon(Icon.createWithResource(this, R.mipmap.ic_launcher))
.setImportant(true)
.build()
// 創(chuàng)建一個(gè)模擬的短信對(duì)話內(nèi)容
val style:Notification.MessagingStyle = Notification.MessagingStyle(personA)
.addMessage("你好句喷,我是鴻鵠镣典!",System.currentTimeMillis(),personA)
.addMessage("你好,我是樹雞唾琼!",System.currentTimeMillis()+1000L,personB)
.setGroupConversation(true)
//創(chuàng)建通知渠道
val channel = NotificationChannel(CHANNEL_ID, "鴻鵠UI組件庫(kù)", NotificationManager.IMPORTANCE_HIGH)
notifyMgr?.createNotificationChannel(channel)
val builder = Notification.Builder(this, CHANNEL_ID)
.setContentTitle("androidR氣泡體驗(yàn)")
.setContentText("測(cè)試android Q 版本的樣式通知")
.setContentIntent(contentIntent)
.setSmallIcon(R.mipmap.ic_launcher)
.setBubbleMetadata(bubbleData)
.addPerson(personA)
.setStyle(style)
.setAutoCancel(true)
.setShowWhen(true)
notifyMgr?.notify(notificationID, builder.build())
}
輔助方法兄春,判斷當(dāng)前系統(tǒng)是否支持氣泡顯示
fun canBubble(): Boolean? {
val channel = (if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
notifyMgr!!.getNotificationChannel(CHANNEL_ID)
} else {
null
}) ?: return false
return if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q) {
notifyMgr!!.areBubblesAllowed() && channel.canBubble()
} else {
false
}
}
四 運(yùn)行截圖
Screenshot_1602749008.png
Screenshot_1602732589.png
注釋:相關(guān)代碼及配置均自測(cè)OK,但手頭只有模擬器 锡溯,暫無(wú)真機(jī)赶舆,通知欄的通知長(zhǎng)安或配置后無(wú)法切換到 氣泡樣式,各種配置也反復(fù)檢查了多遍祭饭,暫時(shí)借用他人運(yùn)行后的氣泡截圖
android-r-bubble-demo.png