Connectivitymanager.type_wifi Is Showing Deprecated In Code.i Had Use Network Capabilities In Above M Version , Want To Remove Warning Of Deprecated
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 C
Solution 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
classMain2Activity : AppCompatActivity() {
    overridefunonCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main2)
        val networkResult = getConnectionType(this)
    }
    @Suppress("DEPRECATION")fungetConnectionType(context: Context): Boolean {
        var result = falseval 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
                    } elseif (hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) {
                        result = true
                    }
                }
            }
        } else {
            cm?.run {
                cm.activeNetworkInfo?.run {
                    if (type == ConnectivityManager.TYPE_WIFI) {
                        result = true
                    } elseif (type == ConnectivityManager.TYPE_MOBILE) {
                        result = true
                    }
                }
            }
        }
        return result
    }
}
CHECK THE SCREENSHOT

Post a Comment for "Connectivitymanager.type_wifi Is Showing Deprecated In Code.i Had Use Network Capabilities In Above M Version , Want To Remove Warning Of Deprecated"