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()
        }
    }
}

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

相关推荐
练习本3 小时前
Android系统架构模式分析
android·java·架构·系统架构
每次的天空8 小时前
Kotlin 内联函数深度解析:从源码到实践优化
android·开发语言·kotlin
练习本9 小时前
Android MVC架构的现代化改造:构建清晰单向数据流
android·架构·mvc
早上好啊! 树哥9 小时前
android studio开发:设置屏幕朝向为竖屏,强制应用的包体始终以竖屏(纵向)展示
android·ide·android studio
YY_pdd10 小时前
使用go开发安卓程序
android·golang
Android 小码峰啊12 小时前
Android Compose 框架物理动画之捕捉动画深入剖析(29)
android·spring
bubiyoushang88812 小时前
深入探索Laravel框架中的Blade模板引擎
android·android studio·laravel
cyy29812 小时前
android 记录应用内存
android·linux·运维
CYRUS STUDIO12 小时前
adb 实用命令汇总
android·adb·命令模式·工具
这儿有一堆花12 小时前
安卓应用卡顿、性能低下的背后原因
android·安卓