「Android/kotlin/net」の版間の差分
提供: 初心者エンジニアの簡易メモ
細 (Admin がページ「Android/kotlin/wifi」を「Android/kotlin/net」に、リダイレクトを残さずに移動しました) |
|||
(同じ利用者による、間の1版が非表示) | |||
行11: | 行11: | ||
val connectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager | val connectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { | ||
− | + | val networkCapabilities = | |
connectivityManager.getNetworkCapabilities(connectivityManager.activeNetwork) | connectivityManager.getNetworkCapabilities(connectivityManager.activeNetwork) | ||
− | + | return networkCapabilities?.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) | |
?: false | ?: false | ||
} else { | } else { |
2021年2月9日 (火) 15:58時点における最新版
wifi判定
val networkInfo: NetworkInfo? = cm.activeNetworkInfo (networkInfo != null && networkInfo.isConnected networkInfo.type == ConnectivityManager.TYPE_WIFI))
参考:https://www.fixes.pub/program/71288.html
net判定
val connectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { val networkCapabilities = connectivityManager.getNetworkCapabilities(connectivityManager.activeNetwork) return networkCapabilities?.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) ?: false } else { val networkInfo: NetworkInfo? = connectivityManager.activeNetworkInfo return (networkInfo != null && networkInfo.isConnected) }
API23以上は ConnectivityManagerのactiveNetworkが使えると思うので、そちらを使う。