0

I just started learning Kotlin,I am currently working on a WebView based app and I need to show a alert box if Internet is not available after the app is open. How can I do that in Kotlin?

Note: I already have checked through many Stack Overflow questions, but all the answers are for java and not Kotlin. Also, I'm just a beginner, So Please write down answer in simple way so I can understand. Sorry for Bad English

MMG
  • 3,226
  • 5
  • 16
  • 43
  • https://stackoverflow.com/questions/1560788/how-to-check-internet-access-on-android-inetaddress-never-times-out there's an example of how to do it with kotlin – a_local_nobody May 19 '20 at 13:00

2 Answers2

2
 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.

SawOnGam
  • 142
  • 3
0
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.net.ConnectivityManager

open class ConnectionCheck:BroadcastReceiver() {

    override fun onReceive(context: Context?, intent: Intent?) {
        if (connectivityReceiverListener != null) {
            connectivityReceiverListener!!.onNetworkConnectionChanged(
                isConnectedOrConnecting(
                    context!!
                )
            )
        }
    }

    private fun isConnectedOrConnecting(context: Context): Boolean {
        val connMgr = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
        val networkInfo = connMgr.activeNetworkInfo
        return networkInfo != null && networkInfo.isConnectedOrConnecting
    }

    interface ConnectivityReceiverListener {
        fun onNetworkConnectionChanged(isConnected: Boolean)
    }

    companion object {
        var connectivityReceiverListener: ConnectivityReceiverListener? = null
    }
}

This is internet connectivity activity. In every activity that you want to check internet connection, that activity should extend ConnectionCheck.ConnectivityReceiverListener.

Don't forget to put <uses-permission android:name="android.permission.INTERNET"/> in manifest.

MMG
  • 3,226
  • 5
  • 16
  • 43
  • I didn't got it. Should I create a different class for it or use it in AppCompatACtivity? – Anderson Spencer May 19 '20 at 12:56
  • Yes, create new class @AndersonSpencer – MMG May 19 '20 at 12:57
  • I'm receiving "Classifier 'ConnectivityReceiverListener' does not have a companion object, and thus must be initialized here" Error. Can't I do this withour creating a separate class and just using showAlert while there's no internet connection? – Anderson Spencer May 19 '20 at 13:22
  • Add override fun onNetworkConnectionChanged(isConnected: Boolean) { } @AndersonSpencer – MMG May 19 '20 at 13:24