gpt4 book ai didi

android - 如何在 Android 应用程序未激活/未聚焦时显示 View ?

转载 作者:行者123 更新时间:2023-12-05 03:16:51 25 4
gpt4 key购买 nike

我想打开我的应用程序并从中查看随处可用的内容(例如小部件),而不仅仅是在打开应用程序时
示例:
enter image description here
请注意,我在桌面页面上,应用程序未打开,但在后台处于 Activity 状态,并在各处显示带有 Hello 文本的“小部件”

最佳答案

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

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

创建一个实现 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)
}
}

注意:如果要显示来自服务的叠加层,可能最好在服务中继承此类。如果你觉得更好,请在评论中写信给我

在 Activity 或服务中添加您的观点

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 应用程序未激活/未聚焦时显示 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74433762/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com