Skip to content

Please dont use google.com for detault. it will starts saying you are robot.... use https://connectivitycheck.gstatic.com/generate_204 #3

@rohan-paudel

Description

@rohan-paudel

private fun checkInternetConnection(completion: (Boolean) -> Unit) {
coroutineScope.launch {
val isConnected = try {
val url = URL(NetworkReachabilityConfig.getTestUrl())
val connection = url.openConnection() as HttpURLConnection
connection.requestMethod = "HEAD"
connection.connectTimeout = 3000 // 3 seconds timeout
connection.readTimeout = 3000 // 3 seconds timeout
connection.connect()
connection.responseCode in 200..399
} catch (e: Exception) {
false
}
withContext(Dispatchers.Main) { completion(isConnected) }
}
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions