Internet availability check for any standard application is necessary. When you call an Rest API or any internet related task execution, you must need to check the internet availability in your application.
So In this article, I show you how to check the internet availability in android | Kotlin
So, First in your project add the INTERNET and ACCESS_NETWORK_STATE permission in menifest.xml
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Make a function in kotlin, that return true if internet connection is available else it return false
fun isOnline(context: Context): Boolean { val cm = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager val activeNetwork: NetworkInfo? = cm.activeNetworkInfo return activeNetwork?.isConnectedOrConnecting == true }
Now, simply you call it in your respective area under if condition
if(isOnline(context)){ //Device connect with Internet }else{ //Device does not connect with Internet }
Please share this article,
Thanks for reading.
Comments
Post a Comment