How to programmatically check availibilty of internet connection in Android?

后端 未结 8 1936
隐瞒了意图╮
隐瞒了意图╮ 2020-12-09 04:02

I want to check programmatically whether there is an internet connection in Android phone/emulator. So that once I am sure that an internet connection is present then I\'ll

8条回答
  •  攒了一身酷
    2020-12-09 04:36

    this code must be run in background thread

    fun hasActiveInternetConnection(): Boolean {
        return try {
            val ipAddr: InetAddress = InetAddress.getByName("google.com")
            !ipAddr.equals("")
        } catch (e: java.lang.Exception) {
            false
        }
    }
    

提交回复
热议问题