private var mConnManager: ConnectivityManager? = null
private var mNetworkCallback: ConnectivityManager.NetworkCallback? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
checkNetwork()
}
override fun onStop() {
super.onStop()
if (mConnManager != null && mNetworkCallback != null) {
mConnManager!!.unregisterNetworkCallback(mNetworkCallback!!)
}
}
// 현재 네트워크가 연결되어 있는 지 여부, 연결되어 있지 않으면 채팅 입력 막음, 재연결 시 채팅 입력 허용
private fun checkNetwork() {
val builder: NetworkRequest.Builder = NetworkRequest.Builder()
mNetworkCallback = object : ConnectivityManager.NetworkCallback() {
override fun onAvailable(network: Network) { // 네트워크 연결됨
super.onAvailable(network)
CLog.i("sheeLog onAvailable")
CoroutineScope(Dispatchers.Main).launch {
binding.etInputText.isEnabled = true
}
}
override fun onLost(network: Network) { // 네트워크 연결 안됨
super.onLost(network)
CLog.i("sheeLog onLost")
CoroutineScope(Dispatchers.Main).launch {
binding.etInputText.isEnabled = false
}
}
}
mConnManager = getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
mConnManager!!.registerNetworkCallback(builder.build(),
mNetworkCallback as ConnectivityManager.NetworkCallback
)
}