0

I want to remove the warning which is showing in the code below m version.I had use below code which is working good but still want to remove the warning which is showing in line ConnectivityManager.TYPE_WIFI.Can below warning be removed which is also showing in kotlin compiler ??

val cm = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager?
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            cm?.run {
                cm.getNetworkCapabilities(cm.activeNetwork)?.run {
                    if (hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) {
                        return true
                    } else if (hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) {
                        return true
                    }
                }
            }
        } else {
            cm?.run {
                cm.activeNetworkInfo?.run {
                    if (type == ConnectivityManager.TYPE_WIFI) {
                        return true
                    } else if (type == ConnectivityManager.TYPE_MOBILE) {
                        return true
                    }
                }
            }
        }

Warning :- 'getter for type: Int' is deprecated.

Ankit Gupta
  • 64
  • 10
  • TL;DR: I don't think you can. Lint currently doesn't check for deprecated code (only deprecated xml attributes&libraries: https://android.googlesource.com/platform/tools/base/+log/studio-master-dev/lint/libs/lint-checks/src/main/java/com/android/tools/lint/checks) so deprecation is checked by Java/Kotlin compiler by looking for @Deprecated annotations. What you need is a check that knows this is a code path for a specific Android version (< M), look into the SDK of that Android version (M) to see if it's deprecated and only then flag it. Would make sense to file a Lint issue imho. – Jeroen Mols Jun 07 '19 at 08:26
  • Yes , i want to change else condition code for version < M and >16 so that warning can be removed and i can check network condition without deprecation warning on 29 version. – Ankit Gupta Jun 07 '19 at 08:50
  • This is a duplicate of https://stackoverflow.com/questions/52816443/what-is-alternative-to-connectivitymanager-type-wifi-deprecated-in-android-p-api – Christian Brüggemann Jun 07 '19 at 15:38
  • I had checked this link before.Please double check the code before commenting it is duplicate.I want to support all version not only above M version which NetworkCapabilities support. – Ankit Gupta Jun 10 '19 at 07:12

1 Answers1

-1

Try this

You can use @Suppress("DEPRECATION") to remove that warning

SAMPLE CODE

import android.content.Context
import android.net.ConnectivityManager
import android.net.NetworkCapabilities
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle

class Main2Activity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main2)

        val networkResult = getConnectionType(this)

    }

    @Suppress("DEPRECATION")
    fun getConnectionType(context: Context): Boolean {
        var result = false
        val cm = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager?
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            cm?.run {
                cm.getNetworkCapabilities(cm.activeNetwork)?.run {
                    if (hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) {
                        result = true
                    } else if (hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) {
                        result = true
                    }
                }
            }
        } else {
            cm?.run {
                cm.activeNetworkInfo?.run {
                    if (type == ConnectivityManager.TYPE_WIFI) {
                        result = true
                    } else if (type == ConnectivityManager.TYPE_MOBILE) {
                        result = true
                    }
                }
            }
        }
        return result
    }
}

CHECK THE SCREENSHOT

enter image description here

AskNilesh
  • 67,701
  • 16
  • 123
  • 163
  • 2
    I don't want to use @Suppress("DEPRECATION").This is not the correct way to do it.I want to change in code or to replace the code so that the warning automatically gone without using deprecation code . – Ankit Gupta Jun 04 '19 at 09:16
  • 1
    @AnkitGupta actually using `@Suppress("DEPRECATION")` is the correct way to handle this. The deprecation is a warning and as long as you support versions having this API and compiling with versions providing the signature, everything is good. If your minimal SDK is set to 21, you could also only use the if-branch, as it was introduced in Android L. – tynn Jun 08 '19 at 12:01