1

I wanted to create an extension function for Android built in ConnectivityManager class so that it can be used statically by any class in my project. I am using Kotlin only.

fun ConnectivityManager.checkInternet(context: Context): Boolean {
        val manager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
        val connection = manager.activeNetworkInfo
        if (connection != null && connection.isConnectedOrConnecting) {
            return true
        }
        return false
    }

I can access this function by creating the object of ConnectivityManager but what i want to do is to use it statically. Any possibility?

Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121
Atif Rehman
  • 325
  • 1
  • 12
  • Does you function use `ConnectivityManager` in its logic? If not, what do you need it to be an extension function for? – voddan Jan 24 '18 at 08:03
  • This should be an extension on `Context`, not `ConnectivityManager`. – Marko Topolnik Jan 24 '18 at 08:34
  • It can go both ways, we can have context or an object of connectivitymanager extracted from context as argument to this function, what I exactly want to know is that can we use extension function approach on kotlin to kotlin classes for Android built in classes in static way. – Atif Rehman Jan 24 '18 at 09:23
  • Possible duplicate of [Static extension methods in Kotlin](https://stackoverflow.com/questions/28210188/static-extension-methods-in-kotlin) – kyay10 Jun 12 '19 at 04:14

3 Answers3

5

This is only possible if your class has a companion object defined:

class MyClass {
    companion object { } 
}

fun MyClass.Companion.foo() {
}

Just like regular members of the companion object, they can be called using only the class name as the qualifier:

MyClass.foo()

Unfortunately, Java classes do not have such companion objects, which is why you can’t achieve what you want.

s1m0nw1
  • 76,759
  • 17
  • 167
  • 196
5

Quite likely this kind of extension function in not what you are looking for. I can see two approaches how you can handle it.

Either create top-level function is separate file (e.g. network.kt).

fun checkInternet(context: Context): Boolean{
    // perform check here
}

Or create extension method on Context

fun Context.checkInternet(): Boolean {
    val manager = this.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
    // I've "kotlinized" a bit your `if` statement
    return manager.activeNetworkInfo?.isConnectedOrConnecting ?: false
}

Personally, I'd prefer the approach with extension on Context.

rafal
  • 3,120
  • 1
  • 17
  • 12
  • Context or ConectivityManager, can we make this approach to access the function statically. Your answer only replace my ConnectivityManager to Context. What should be done to access this function statically from a kotlin class. – Atif Rehman Jan 24 '18 at 09:25
  • In pure Kotlin there's no such think as a "static calling", to "emulate" static methods you can use `companion object`. Why do you need a "static call" and why top-level function or extension on `Context` are not sufficient? – rafal Jan 24 '18 at 10:42
  • "In pure Kotlin there's no such think as a "static calling"" Got my answer here, other approaches are ok too, just wanted to see if it can be done the way I was thinking. – Atif Rehman Jan 24 '18 at 10:57
  • For detail: https://medium.com/@khadijahameed415/extension-function-in-kotlin-e3a3ed5920c4 – Khadija Hameed Feb 02 '23 at 04:53
0

Your function checkInternet is defined as an extension function for your class ConnectivityManager.

If you want to use your function checkInternet like an extension method for any class, you can do this:

fun Any.checkInternet(context: Context): Boolean { val manager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager val connection = manager.activeNetworkInfo if (connection != null && connection.isConnectedOrConnecting) { return true } return false }

and import the checkInternet function where you are using it.

Otherwise, you can make checkInternet as a static member in ConnectivityManager and use it like a static function.

Tolotra Raharison
  • 3,034
  • 1
  • 10
  • 15