0

is there a solution for a popup to appear on all the pages of the application if the user does not have an internet connection ? Thank you

Edric
  • 24,639
  • 13
  • 81
  • 91
admindunet
  • 21
  • 7

2 Answers2

0

You can simply create an activity with an error message. So whenever there's no internet connection then show them no internet connection activity. You can also give a message in a toast or snack bar.

0

You can use this method

fun isNetworkConnected(context: Context) : Boolean {
    val connectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
    val capabilities =  connectivityManager.getNetworkCapabilities(connectivityManager.activeNetwork)
    capabilities.also {
        if (it != null){
            if (it.hasTransport(NetworkCapabilities.TRANSPORT_WIFI))
                return true
            else if (it.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)){
                return true
            }
        }
    }
    return false
}

you can check internet connection every where you want.

    if(isNetworkConnected()){
     // do something
     }

    else{
     // use toast or a dialog to say internet is not connected
     }

and don't forget to add this permissions to manifst

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
Sadegh J
  • 1,603
  • 2
  • 9
  • 22
  • i get : 'getter for activeNetworkInfo: NetworkInfo?' is deprecated. Deprecated in Java 'getter for isAvailable: Boolean' is deprecated. Deprecated in Java 'getter for isConnected: Boolean' is deprecated. Deprecated in Java 'getter for isFailover: Boolean' is deprecated. Deprecated in Java 'getter for isRoaming: Boolean' is deprecated. Deprecated in Java – – admindunet Jan 07 '21 at 17:05
  • @admindunet check sol again – Sadegh J Jan 08 '21 at 09:04