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

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

相关推荐
灿烂阳光g15 小时前
domain_auto_trans,source_domain,untrusted_app
android·linux
低调小一17 小时前
Android传统开发 vs Android Compose vs HarmonyOS ArkUI 对照表
android·华为·harmonyos
雨白17 小时前
Java 多线程指南:从基础用法到线程安全
android·java
00后程序员张18 小时前
详细解析苹果iOS应用上架到App Store的完整步骤与指南
android·ios·小程序·https·uni-app·iphone·webview
程序员江同学19 小时前
ovCompose + AI 开发跨三端的 Now in Kotlin App
android·kotlin·harmonyos
2501_9151063219 小时前
Xcode 上传 ipa 全流程详解 App Store 上架流程、uni-app 生成 ipa 文件上传与审核指南
android·macos·ios·小程序·uni-app·iphone·xcode
消失的旧时光-194319 小时前
Kotlinx.serialization 使用讲解
android·数据结构·android jetpack
灿烂阳光g20 小时前
SELinux 策略文件编写
android·linux
.豆鲨包21 小时前
【Android】Viewpager2实现无限轮播图
android·java