private fun checkConnectivity() {
val manager = this.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
val activeNetwork = manager.activeNetworkInfo
if (null == activeNetwork) {
val dialogBuilder = AlertDialog.Builder(this)
val intent = Intent(this, MainActivity::class.java)
// set message of alert dialog
dialogBuilder.setMessage("Make sure that WI-FI or mobile data is turned on, then try again")
// if the dialog is cancelable
.setCancelable(false)
// positive button text and action
.setPositiveButton("Retry", DialogInterface.OnClickListener { dialog, id ->
recreate()
})
// negative button text and action
.setNegativeButton("Cancel", DialogInterface.OnClickListener { dialog, id ->
finish()
})
// create dialog box
val alert = dialogBuilder.create()
// set title for alert dialog box
alert.setTitle("No Internet Connection")
alert.setIcon(R.mipmap.ic_launcher)
// show alert dialog
alert.show()
}
}
Also add add this permissions to Manifest
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET"/>
This should be enough to show a dialog if there's no Internet Connection in your app.