Android Compose 悬浮窗

今天需要写一个悬浮窗,但是在 Compose 中显示悬浮窗会遇到生命周期问题,我搜了一下,竟然有博主还要付费 才给看,我真的是,开源社会不过是闻道有先后 ,大家互帮互助才有未来,这样明目张胆的放几张收款码让付款才给看几行代码,至于吗?吃相如此难看!看看源码还是可以翻出来的,这里免费给大家分享,开源万岁!我的文章不要求关注,不要求付费,不要求会员,只求一个点赞认可,感谢各位。

今天遇到的问题是两个报错,相信大家主要难以解决的也是这两个问题:

kotlin 复制代码
ViewTreeLifecycleOwner not found from androidx.compose.ui.platform.ComposeView
kotlin 复制代码
Composed into the View which doesn't propagateViewTreeSavedStateRegistryOwner!

解决办法:

groovy 复制代码
implementation("androidx.lifecycle:lifecycle-service:2.5.1")
kotlin 复制代码
import android.graphics.PixelFormat
import android.view.WindowManager
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.ComposeView
import androidx.lifecycle.LifecycleService
import androidx.lifecycle.setViewTreeLifecycleOwner
import androidx.savedstate.SavedStateRegistry
import androidx.savedstate.SavedStateRegistryController
import androidx.savedstate.SavedStateRegistryOwner
import androidx.savedstate.setViewTreeSavedStateRegistryOwner
import com.puremic.karaoke.global.tuning.TuningItemNotifier
import com.puremic.karaoke.ui.theme.PuremicKaraokeTheme

class NotifierService: LifecycleService(), SavedStateRegistryOwner {
    private val savedStateRegistryController by lazy {
        SavedStateRegistryController.create(this)
    }

    override val savedStateRegistry: SavedStateRegistry
        get() = savedStateRegistryController.savedStateRegistry

    override fun onCreate() {
        super.onCreate()
        savedStateRegistryController.performAttach()
        savedStateRegistryController.performRestore(null)

        val windowManager = getSystemService(WindowManager::class.java)
        val params = WindowManager.LayoutParams(
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
            PixelFormat.TRANSLUCENT
        )

        val composeView = ComposeView(this).apply {
            setViewTreeLifecycleOwner(this@NotifierService)
            setViewTreeSavedStateRegistryOwner(this@NotifierService)
            setContent {
                PuremicKaraokeTheme {
                    Notifier()
                }
            }
        }
        windowManager.addView(composeView, params)
    }

    @Composable
    private fun Notifier() {
        Box(
            modifier = Modifier.fillMaxSize()
        ) {
            TuningItemNotifier()
        }
    }
}

解决问题的无非就是那三四行代码,祝大家学业有成,步步高升

相关推荐
木子庆五几秒前
Android设计模式之代理模式
android·设计模式·代理模式
在雨季等你17 分钟前
创业之旅 - 反思 - 整改 - 新的方向 - 诚邀
android
Long_poem32 分钟前
【自学笔记】PHP语言基础知识点总览-持续更新
android·笔记·php
fatiaozhang95272 小时前
晶晨S905L3A(B)-安卓9.0-开启ADB和ROOT-支持IPTV6-支持外置游戏系统-支持多种无线芯片-支持救砖-完美通刷线刷固件包
android·游戏·adb·华为·电视盒子·机顶盒rom·魔百盒固件
行墨3 小时前
Kotlin语言的==与===比较操作
android
圣火喵喵教3 小时前
Pixel 8 pro 刷AOSP源码 Debug 详细教程(含救砖)
android
二流小码农4 小时前
鸿蒙开发:使用Ellipse绘制椭圆
android·ios·harmonyos
自不量力的A同学4 小时前
谷歌将 Android OS 完全转变为 “内部开发”
android
行墨4 小时前
Kotlin 的可空类型
android
suren4 小时前
deepseek ai 输入法
android