-4

I want to open another activity which displays no internet connection if there is no network. Can I do that? please help.

Zoe
  • 27,060
  • 21
  • 118
  • 148
Nasim
  • 125
  • 2
  • 12
  • Simply add the code from [here](https://stackoverflow.com/a/34175890/4409113) in your class & check with `if(isConnected()) {startActivity(...)}` or don't start or whatever... – ʍѳђઽ૯ท Sep 14 '18 at 12:41

1 Answers1

1
inline fun Context.withNetwork(isToNotify: Boolean = true, block: () -> Unit) {
    val connectivityManager = this.getSystemService(Context.CONNECTIVITY_SERVICE) as? ConnectivityManager
    connectivityManager?.let {
        val netInfo = it.activeNetworkInfo
        val isConnected = netInfo != null && netInfo.isConnectedOrConnecting
        if (isToNotify && !isConnected){
            toast(R.string.message_no_network_message)
             //Use Here Intent For Another Activity

        }else{
            block()
        }
    }
}
AskNilesh
  • 67,701
  • 16
  • 123
  • 163