Android 检查是否有网络
@Suppress("DEPRECATION") private fun checkIfOnline(): Boolean { val cm = getSystemService(context, ConnectivityManager::class.java) return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { val capabilities = cm?.getNetworkCapabilities(cm.activeNetwork) ?: return false capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) && capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED) } else { cm?.activeNetworkInfo?.isConnectedOrConnecting == true } }