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

How to get instance in Kotlin with Context.getSystemService(Class)

在 Google Android Kotlin 文档中, android 文档中不时出现以下一行: 这个 class 的实例必须使用 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.

谁能解释一下这是什么以及如何为 class AppOpsManager.

创建一个实例

通常我们可以创建这样的实例: val use = AppOpsManager()

请帮忙解释以上Context.getSystemService().

谢谢。

使用以下内容:

context.getSystemService(AppOpsManager::class.java)

来自 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.

要创建此 class 的实例,您必须从上下文实例中使用 getSystemService

val appOpsManager: AppOpsManager? = getSystemService(Context.APP_OPS_SERVICE) as AppOpsManager?

如果您的 minSdkVersion 是 23,那么您可以使用此代码。

val appOpsManager: AppOpsManager? = getSystemService(AppOpsManager::class.java)