9

In Google Android Kotlin documentation, Every now and then there is a below line present in android documentation that: Instances of this class must be obtained using Context.getSystemService(Class)

For example:

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.

Can someone please clarify what this is and how do I create a instance for class AppOpsManager.

Usually we can create instance like: val use = AppOpsManager()

Please help and explain the above Context.getSystemService().

Thank You.

0

3 Answers 3

11

From Android Developer documentation:

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.

To create an instance of this class you must use getSystemService from a context instance.

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

If your minSdkVersion is 23 then you can use this code instead.

val appOpsManager: AppOpsManager? = getSystemService(AppOpsManager::class.java)
Sign up to request clarification or add additional context in comments.

2 Comments

While the docs say that getSystemService (Class<T> serviceClass) has been added in API level 23, it actually works in older Android versions as long as your targetSdkVersion is 23 or higher.
I cannot confirm what Cristian writes, when using the class based variant with minSdkVersion < 23, Android Studio shows an error message.
4

Use the following:

context.getSystemService(AppOpsManager::class.java)

Comments

0
val aom = getSystemService(context, AppOpsManager::class.java)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.