0

Getter for activeNetworkInfo is deprecated, Deprecated in Java. How to fix this?

I used code below, but the android studio tells me that 'activeNetworkInfo' is deprecated.

Application Manifest:

minSdkVersion 21
targetSdkVersion 29

@Provides
    @Singleton
    fun provideIsNetworkAvailable(application: Application): Boolean {
        var isConnected = false
        val connectivityManager = application.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            val networkCapabilities = connectivityManager.activeNetwork ?: return false
            val actNw = connectivityManager.getNetworkCapabilities(networkCapabilities) ?: return false
            isConnected = when {
                actNw.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) -> true
                actNw.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) -> true
                actNw.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET) -> true
                else -> false
            }
        } else {
            connectivityManager.run {
                connectivityManager.activeNetworkInfo?.run {
                    isConnected = when (type) {
                        ConnectivityManager.TYPE_WIFI -> true
                        ConnectivityManager.TYPE_MOBILE -> true
                        ConnectivityManager.TYPE_ETHERNET -> true
                        else -> false
                    }
                }
            }
        }
        return isConnected
    }
Benfactor
  • 543
  • 4
  • 11
  • 31

2 Answers2

0

Use this , its works fine in

@RequiresApi(Build.VERSION_CODES.M)
    fun provideIsNetworkAvailable(application: Application): Boolean{
        private val applicationContext = application.applicationContext
        val connectivityManager = applicationContext.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
        val activeNetwork = connectivityManager.activeNetwork

        connectivityManager.getNetworkCapabilities(activeNetwork).also {
            return it!= null && it.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
        }
    }
rizerphe
  • 1,340
  • 1
  • 14
  • 24
aksBD
  • 189
  • 1
  • 6
  • Please properly format code, [click here to learn how](https://wordpress.com/support/markdown-quick-reference/). – rizerphe May 09 '20 at 06:25
  • While this code may solve the question, [including an explanation](//meta.stackexchange.com/q/114762) of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please [edit] your answer to add explanations and give an indication of what limitations and assumptions apply. – rizerphe May 09 '20 at 06:28
0

I'm using this code :

private fun isInternetAvailable(context: Context): Boolean {
        var result = false
        val connectivityManager =
            context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            val networkCapabilities = connectivityManager.activeNetwork ?: return false
            val actNw =
                connectivityManager.getNetworkCapabilities(networkCapabilities) ?: return false
            result = when {
                actNw.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) -> true
                actNw.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) -> true
                actNw.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET) -> true
                else -> false
            }
        } else {
            connectivityManager.run {
                connectivityManager.activeNetworkInfo?.run {
                    result = when (type) {
                        ConnectivityManager.TYPE_WIFI -> true
                        ConnectivityManager.TYPE_MOBILE -> true
                        ConnectivityManager.TYPE_ETHERNET -> true
                        else -> false
                    }

                }
            }
        }

        return result
    }

You can view the address below to get detailed information.

activeNetworkInfo.type is deprecated in API level 28

Umit Kose
  • 1
  • 3