Skip to content

ContextExtensions

Birju Vachhani edited this page Sep 26, 2019 · 3 revisions

Examples

Get Service

val inputManager = getService<InputMethodManager>(Context.INPUT_METHOD_SERVICE)

All the casting is done implicitly for you in a safe way. Returns the service or null if the name does not exist.

If your app's minSDK is 23, then you can use this:

val activityManager: ActivityManager? = context.getServiceOrNull() // returns service instance or null
val notificationManger: NotificationManager = context.getService() // returns non-null service instance, throws exception if not found

Get casted Application class

application<MyApp>()

If you'll not behave then you'll get an exception thrown right into your face. (ClassCastException)

Internet Check

hasInternet() // returns Boolean

Get LayoutInflater right away!

layoutInflater // yes, it's a property!

Get Quality of the Internet Service.

This extension can be used to know which type of internet connectivity is being used.

Possible types are Not Connected, WIFI, 2G, 3G, 4G and 5G.

context.getQualityOfService() // requires Android Lollipop or higher

For more information, look at the ContextExtensions.kt file.

Clone this wiki locally