当 Android 应用程序未激活/未聚焦时如何显示视图?

Fre*_*888 5 android android-view kotlin android-jetpack-compose

我想打开我的应用程序并从中看到一些东西(例如小部件),这些东西在任何地方都可用,而不仅仅是在打开应用程序时
示例:
在此输入图像描述
请注意,我在桌面页面上,应用程序未打开,但在后台处于活动状态,并在Hello各处显示带有文本的“小部件”

Fre*_*888 6

在AndroidManifest.xml中添加android.permission.SYSTEM_ALERT_WINDOW权限

// 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>

Run Code Online (Sandbox Code Playgroud)

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

创建一个正在实现的类SavedStateRegistryOwner

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)
    }
}
Run Code Online (Sandbox Code Playgroud)

注意:如果您要显示服务的叠加层,最好在服务中继承此类。如果您认为更好,请在评论中给我留言

在活动或服务中添加您的视图

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)
Run Code Online (Sandbox Code Playgroud)