判断有无网络连接
object NetworkUtils {
fun isNetworkAvailable(): Boolean{
val connMgr = BaseApplication.application.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
val networkCapabilities = connMgr.getNetworkCapabilities(connMgr.activeNetwork)
return networkCapabilities?.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED) ?: false
} else {
val networkInfo = connMgr.activeNetworkInfo
return networkInfo?.isConnected ?: false
}
}
}
网络变换实时监听
object NetworkUtils {
fun setNetworkChangeListener(context: Context?){
val connMgr = BaseApplication.application