Archives
Trending
Support
Login
clear text
XML
Django
JavaScript
MATLAB
C
C++
C#
Python
SQL
Shell
Bash
Markdown
YAML
JSON
HTML
CSS
PHP
Java
Ruby
Go
Rust
Swift
Kotlin
Arduino
TypeScript
Perl
Autohotkey
Lua
SQF
R
Scala
Haskell
Groovy
Dart
Clojure
VB.NET
Objective-C
PowerShell
Bash
CoffeeScript
Verilog
class NetworkChecker(private val context: Context) { // provided by Koin private val connectivityManager by lazy { context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager } // Quick check for potential connectivity fun isNetworkConnected(): Boolean { val network = connectivityManager.activeNetwork ?: return false val capabilities = connectivityManager.getNetworkCapabilities(network) ?: return false return when { capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) -> true capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) -> true capabilities.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET) -> true else -> false } } // Actual internet connectivity check suspend fun hasInternetAccess(timeout: Long = 3000L): Boolean { return withContext(Dispatchers.IO) { if (!isNetworkConnected()) return@withContext false try { withTimeout(timeout) { val url = URL("http://example.com") val urlConnection = url.openConnection() as HttpURLConnection urlConnection.setRequestProperty("Connection", "close") urlConnection.connectTimeout = timeout.toInt() urlConnection.connect() urlConnection.responseCode == 200 || urlConnection.responseCode == 204 } } catch (e: Exception) { Timber.e("hasInternetAccess exception: $e") false } } } }
Mark as private (unlisted)
for 30 minutes
for 6 hours
for 1 day
for 1 week
for 1 month
for 1 year
everlasting (like CCCP)