0

I am working on an app which uses Android API 28, but I am having an issue with the app not always recognizing that the phone has internet access. However, other apps on the phone is working. The function I used to check the internet connection is:

fun checkInternetConnection(): Boolean { val connectivityManager = getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager val networkInfo = connectivityManager.isDefaultNetworkActive return networkInfo } 
1
  • Is your problem resolved Commented Sep 30, 2019 at 20:09

2 Answers 2

1

Use this code

fun hasNetwork(context: Context): Boolean { var isConnected: Boolean = false val connectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager val activeNetwork: NetworkInfo? = connectivityManager.activeNetworkInfo if (activeNetwork != null && activeNetwork.isConnected) isConnected = true return isConnected } 

Also give permission in menifest

 <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> 
Sign up to request clarification or add additional context in comments.

Comments

0

Did you give permissions in manifest?

<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.