Created
January 4, 2018 06:58
-
-
Save abhinav272/9d0a9367ace045af8f043288237edcd8 to your computer and use it in GitHub Desktop.
Network Util file for checking network availability and strength written in kotlin
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.telephony.TelephonyManager | |
import java.io.IOException | |
import java.net.HttpURLConnection | |
import java.net.NetworkInterface | |
import java.net.SocketException | |
import java.net.URL | |
/** | |
* Created by abhinav.sharma on 03/11/17. | |
*/ | |
class NetworkUtil{ | |
companion object { | |
var NET_CNNT_OK = 1 // NetworkAvailable | |
var NET_CNNT_TIMEOUT = 2 // no NetworkAvailable | |
var NET_NOT_PREPARE = 3 // Net no ready | |
var NET_ERROR = 4 //net error | |
private val TIMEOUT = 3000 // TIMEOUT | |
/** | |
* check NetworkAvailable | |
* | |
* @param context | |
* @return | |
*/ | |
@JvmStatic | |
fun isNetworkAvailable(context: Context): Boolean { | |
val manager = context.applicationContext.getSystemService( | |
Context.CONNECTIVITY_SERVICE) as ConnectivityManager | |
val info = manager.activeNetworkInfo | |
return !(null == info || !info.isAvailable) | |
} | |
/** | |
* 得到ip地址 | |
* | |
* @return | |
*/ | |
@JvmStatic | |
fun getLocalIpAddress(): String { | |
var ret = "" | |
try { | |
val en = NetworkInterface.getNetworkInterfaces() | |
while (en.hasMoreElements()) { | |
val enumIpAddress = en.nextElement().inetAddresses | |
while (enumIpAddress.hasMoreElements()) { | |
val netAddress = enumIpAddress.nextElement() | |
if (!netAddress.isLoopbackAddress) { | |
ret = netAddress.hostAddress.toString() | |
} | |
} | |
} | |
} catch (ex: SocketException) { | |
ex.printStackTrace() | |
} | |
return ret | |
} | |
/** | |
* ping "http://www.google.com" | |
* | |
* @return | |
*/ | |
@JvmStatic | |
private fun pingNetWork(): Boolean { | |
var result = false | |
var httpUrl: HttpURLConnection? = null | |
try { | |
httpUrl = URL("http://www.google.com") | |
.openConnection() as HttpURLConnection | |
httpUrl.connectTimeout = TIMEOUT | |
httpUrl.connect() | |
result = true | |
} catch (e: IOException) { | |
} finally { | |
if (null != httpUrl) { | |
httpUrl.disconnect() | |
} | |
} | |
return result | |
} | |
/** | |
* check is3G | |
* | |
* @param context | |
* @return boolean | |
*/ | |
@JvmStatic | |
fun is3G(context: Context): Boolean { | |
val connectivityManager = context | |
.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager | |
val activeNetInfo = connectivityManager.activeNetworkInfo | |
return activeNetInfo != null && activeNetInfo.type == ConnectivityManager.TYPE_MOBILE | |
} | |
/** | |
* isWifi | |
* | |
* @param context | |
* @return boolean | |
*/ | |
@JvmStatic | |
fun isWifi(context: Context): Boolean { | |
val connectivityManager = context | |
.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager | |
val activeNetInfo = connectivityManager.activeNetworkInfo | |
return activeNetInfo != null && activeNetInfo.type == ConnectivityManager.TYPE_WIFI | |
} | |
/** | |
* is2G | |
* | |
* @param context | |
* @return boolean | |
*/ | |
@JvmStatic | |
fun is2G(context: Context): Boolean { | |
val connectivityManager = context | |
.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager | |
val activeNetInfo = connectivityManager.activeNetworkInfo | |
return activeNetInfo != null && (activeNetInfo.subtype == TelephonyManager.NETWORK_TYPE_EDGE | |
|| activeNetInfo.subtype == TelephonyManager.NETWORK_TYPE_GPRS || activeNetInfo | |
.subtype == TelephonyManager.NETWORK_TYPE_CDMA) | |
} | |
/** | |
* is wifi on | |
*/ | |
@JvmStatic | |
fun isWifiEnabled(context: Context): Boolean { | |
val mgrConn = context | |
.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager | |
val mgrTel = context | |
.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager | |
return mgrConn.activeNetworkInfo != null && mgrConn | |
.activeNetworkInfo.state == NetworkInfo.State.CONNECTED || mgrTel | |
.networkType == TelephonyManager.NETWORK_TYPE_UMTS | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment