gpt4 book ai didi

android - 如何使用 Context.getSystemService(Class) 在 Kotlin 中获取实例

转载 作者:行者123 更新时间:2023-12-04 23:47:45 37 4
gpt4 key购买 nike

在 Google Android Kotlin 文档中,
android 文档中时不时会出现以下一行:
必须使用 Context.getSystemService(Class) 获取此类的实例

例如:

Instances of this class must be obtained using Context.getSystemService(Class) with the argument AppOpsManager.class or Context.getSystemService(String) with the argument Context.APP_OPS_SERVICE.



有人可以澄清这是什么以及如何为类 AppOpsManager 创建一个实例吗? .

通常我们可以创建如下实例: val use = AppOpsManager()
请帮忙解释一下以上 Context.getSystemService() .

谢谢你。

最佳答案

来自 Android Developer文档:

AppOpsManager

API for interacting with "application operation" tracking.

This API is not generally intended for third party application developers; most features are only available to system applications.

Instances of this class must be obtained using Context.getSystemService(Class) with the argument AppOpsManager.class or Context.getSystemService(String) with the argument Context.APP_OPS_SERVICE.



要创建此类的实例,您必须使用 getSystemService从上下文实例。
val appOpsManager: AppOpsManager? = getSystemService(Context.APP_OPS_SERVICE) as AppOpsManager?

如果您的 minSdkVersion是 23,那么您可以改用此代码。
val appOpsManager: AppOpsManager? = getSystemService(AppOpsManager::class.java)

关于android - 如何使用 Context.getSystemService(Class) 在 Kotlin 中获取实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54909870/

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