private fun isNetworkConnected(): Boolean {
val connectivityManager = getSystemService(ConnectivityManager::class.java)
val currentNetwork = connectivityManager.activeNetwork
val caps = connectivityManager.getNetworkCapabilities(currentNetwork) ?: return false
return when {
caps.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) -> true
caps.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) -> true
caps.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET) -> true
else -> false
}
}
val connectivityManager = getSystemService(ConnectivityManager::class.java)
connectivityManager.registerDefaultNetworkCallback(object : ConnectivityManager.NetworkCallback() {
override fun onAvailable(network : Network) {
Log.e(TAG, "The default network is now: " + network)
}
override fun onLost(network : Network) {
Log.e(TAG, "The application no longer has a default network. The last default network was " + network)
}
override fun onCapabilitsChanged(network : Network, networkCapabilities : NetworkCapabilities) {
Log.e(TAG, "The default network changed capabilities: " + networkCapabilities)
}
override fun onLinkPropertiesChanged(network : Network, linkProperties : LinkProperties) {
Log.e(TAG, "The default network changed link properties: " + linkProperties)
}
})
private fun showAlert(){
val builder = AlertDialog.Builder(this)
builder.setTitle("")
.setMessage("네트워크에 연결되지 않았습니다.")
.setPositiveButton("다시 시도하기",
DialogInterface.OnClickListener { dialog, id ->
// 앱 처음부터 다시시작
val intent = Intent(applicationContext, SplashActivity::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_NEW_TASK)
startActivity(intent)
finish()
})
builder.show()
}