I want to open my app and see something from it (for example a widget) that will be available everywhere, not only when the app is opened
Example:
enter image description here
Note that I am on the desktop page, and app is not opened, but in is active in background and shows this "widget" with Hello text everywhere

推荐答案

Add 100 permission to AndroidManifest.xml

// AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.freephoenix888.savemylife"> 
    // Something else

    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
    // Something else
</manifest>

注意:不要忘记向用户请求此权限 Documentation

Create a class that is implementing 100

internal class MyLifecycleOwner : SavedStateRegistryOwner {

    private var mLifecycleRegistry: LifecycleRegistry = LifecycleRegistry(this)
    private var mSavedStateRegistryController: SavedStateRegistryController = SavedStateRegistryController.create(this)

    val isInitialized: Boolean
        get() = true
    override val savedStateRegistry: SavedStateRegistry
        get() = mSavedStateRegistryController.savedStateRegistry

    override fun getLifecycle(): Lifecycle {
        return mLifecycleRegistry
    }

    fun setCurrentState(state: Lifecycle.State) {
        mLifecycleRegistry.currentState = state
    }

    fun handleLifecycleEvent(event: Lifecycle.Event) {
        mLifecycleRegistry.handleLifecycleEvent(event)
    }

    fun performRestore(savedState: Bundle?) {
        mSavedStateRegistryController.performRestore(savedState)
    }

    fun performSave(outBundle: Bundle) {
        mSavedStateRegistryController.performSave(outBundle)
    }
}

注意:如果要显示来自服务的覆盖,那么在服务中继承这个类可能会更好.如果你觉得这样更好的话,给我写 comments

Add your view in an activity or a service

val layoutFlag: Int = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY
} else {
    WindowManager.LayoutParams.TYPE_PHONE
}

val params = WindowManager.LayoutParams(
    WindowManager.LayoutParams.WRAP_CONTENT,
    WindowManager.LayoutParams.WRAP_CONTENT,
    layoutFlag,
    WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE or WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN or WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
    PixelFormat.TRANSLUCENT
)

/* For views (not compose views)
val floatView = (getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater).inflate(R.layout.view_service_float, null)
*/

val composeView = ComposeView(this)
composeView.setContent {
    Text(
        text = "Hello",
        color = Color.Black,
        fontSize = 50.sp,
        modifier = Modifier
            .wrapContentSize()
            .background(Color.Green)
    )
}

val lifecycleOwner = MyLifecycleOwner()
lifecycleOwner.performRestore(null)
lifecycleOwner.handleLifecycleEvent(Lifecycle.Event.ON_CREATE)
ViewTreeLifecycleOwner.set(composeView, lifecycleOwner)
composeView.setViewTreeSavedStateRegistryOwner(lifecycleOwner)
val viewModelStore = ViewModelStore()
ViewTreeViewModelStoreOwner.set(composeView) { viewModelStore }
val coroutineContext = AndroidUiDispatcher.CurrentThread
val runRecomposeScope = CoroutineScope(coroutineContext)
val recomposer = Recomposer(coroutineContext)
composeView.compositionContext = recomposer
runRecomposeScope.launch {
    recomposer.runRecomposeAndApplyChanges()
}

windowManager.addView(composeView, params)

Android相关问答推荐

如何允许我的应用程序在Android 10上运行,同时目标是API 33

将动作传递给嵌套的可组合物

Android配置设置. gradle不同应用风格

Android:MethodHandle. invoke和MethodHandle. invokeExact仅从Android O( - min—api 26)开始支持

使用Retrofit2的API调用:我如何能够一直进行API调用,以更新数据而无需重新打开应用程序

懒惰的垂直网格中盒子的重量-Jetpack组合

Jetpack Compose:如何将浮动操作按钮上方的子按钮居中对齐?

避免在按下肯定按钮时自动取消AlertDialog

Android 14上的慢速意图广播交付

在Jetpack Compose中的隐藏状态栏后面绘制

如何使用进度条和返回函数进行API调用,同时在Android上使用Kotlin保持高效?

修复报错 RecyclerView: No adapter attached;跳过布局

如何绘制内边框?

Jetpack compose (Glance) 小部件在加载位图图像后不会重新组合

在 Jetpack Compose 中包装内容

在compose中,为什么修改List元素的属性,LazyColumn不刷新

可组合的可见性不随状态变化而变化

优化 Room 数据库迁移

在 Android Studio 中替换字符串中的 "

如何将新的 ComponentActivity 与 ViewBinding 和其他旧的 AppCompatActivity 组件一起使用