Android - Programmatically check internet connection and display dialog if notConnected

后端 未结 14 2514
予麋鹿
予麋鹿 2020-12-15 04:04

I am working on a live project. and when user click on the app. the welcome screen appears(there is a webview on that screen). and if the internet is not connected then the

14条回答
  •  我在风中等你
    2020-12-15 05:04

    An Updated Kotlin and Android 29 Version (getNetworkInfo is deprecated in SDK 29) on how to check for internet connection, works for minSDK >= 23 :

    fun hasInternet(): Boolean {
        val connectivityManager = appContainer.contextProvider.currentAppContext()
            .getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
        val network = connectivityManager.activeNetwork
    
        val capabilities = connectivityManager.getNetworkCapabilities(network)
        var hasInternet = false
        capabilities?.let {
            hasInternet = it.hasCapability(NET_CAPABILITY_INTERNET)
        }
        return hasInternet
    }
    

提交回复
热议问题