Last active
December 6, 2017 15:28
-
-
Save Jthomas54/176b103bbc565ec63edcfc66f7ff52df to your computer and use it in GitHub Desktop.
Helpers for checking network connectivity on Android
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.Context | |
import android.net.ConnectivityManager | |
import android.net.NetworkInfo | |
import android.os.Handler | |
import android.os.Looper | |
import java.net.URL | |
import java.net.UnknownHostException | |
import java.util.concurrent.Executors | |
import javax.net.ssl.HttpsURLConnection | |
/** | |
* Utilities for checking a devices network connections | |
*/ | |
object Connectivity { | |
private const val REQUEST_GET = "GET" | |
//You can decrease this timeout to fail faster | |
private const val CONNECT_TIMEOUT = 3 * 1000 //3 Seconds | |
//You can change the url below to change what host is contacted when checking for internet connectivity | |
private const val INTERNET_HOST = "http://clients3.google.com/generate_204" | |
private val UI_HANDLER = Handler(Looper.getMainLooper()) | |
private val EXECUTOR = Executors.newSingleThreadExecutor() | |
/** | |
* Determines if the device has an active network connection | |
* @param ctx | |
* @return true if connected or connecting, false otherwise | |
*/ | |
fun hasNetworkConnection(ctx: Context): Boolean { | |
val networkInfo = getActiveNetworkInfo(ctx) | |
return networkInfo != null && networkInfo.isConnectedOrConnecting | |
} | |
/** | |
* Determines if the device is using an wifi connection | |
* @param ctx | |
* @return true if connected via wifi, false otherwise | |
*/ | |
fun wifiConnected(ctx: Context): Boolean { | |
val networkInfo = getActiveNetworkInfo(ctx) | |
return hasNetworkConnection(ctx) && networkInfo!!.type == ConnectivityManager.TYPE_WIFI | |
} | |
/** | |
* Determines if the the device has access to the internet | |
* <p> | |
* This work is performed on a background thread and the result delivered to the UI thread | |
*/ | |
fun hasInternetAccess(ctx: Context, callback: (isConnected: Boolean) -> Unit) { | |
var reachable = false | |
if (!hasNetworkConnection(ctx)) { | |
UI_HANDLER.post { callback(reachable) } | |
return | |
} | |
//Run check on background thread | |
EXECUTOR.submit({ | |
try { | |
val url = URL(INTERNET_HOST) | |
val connection = url.openConnection() as HttpURLConnection | |
connection.requestMethod = REQUEST_GET | |
connection.connectTimeout = CONNECT_TIMEOUT | |
connection.connect() | |
connection.disconnect() | |
reachable = true | |
} catch (ex: UnknownHostException) { | |
} | |
//Post results back to the UI thread | |
UI_HANDLER.post { callback(reachable) } | |
}) | |
} | |
private fun getActiveNetworkInfo(ctx: Context): NetworkInfo? { | |
val connMgr = ctx.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager | |
return connMgr.activeNetworkInfo | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.BroadcastReceiver | |
import android.content.Context | |
import android.content.Intent | |
import android.content.IntentFilter | |
import android.net.ConnectivityManager | |
import com.your.namespace.utils.Connectivity | |
class NetworkConnectivityBroadcastReceiver(val listener: NetworkConnectivityChangedListener) : BroadcastReceiver() { | |
private val intentFilter: IntentFilter = IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION) | |
//When registering the receiver, Android seems to immediately notify of the connectivity state | |
private var ignoreFirstBroadcast = true | |
fun register(ctx: Context) { | |
ignoreFirstBroadcast = true | |
ctx.registerReceiver(this, intentFilter) | |
} | |
fun unregister(ctx: Context) { | |
ctx.unregisterReceiver(this) | |
} | |
override fun onReceive(ctx: Context, intent: Intent) { | |
if (!ignoreFirstBroadcast) { | |
Connectivity.hasInternetAccess(ctx, { isConnected -> | |
listener.onConnectivityChanged(isConnected) | |
}) | |
} | |
ignoreFirstBroadcast = false | |
} | |
interface NetworkConnectivityChangedListener { | |
fun onConnectivityChanged(hasInternetConnection: Boolean) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment