I have made an android studio project, but I need to check for internet connectivity during each user interaction. I know the usage of the connectivity manager, and several other functions, but this is needed to be implemented in each activity. Can anyone provide any better solution or any better idea. So that the application shows a dialog box whenever the internet goes off.
Asked
Active
Viewed 832 times
1 Answers
0
You may use this method
boolean connected = false;
ConnectivityManager connectivityManager = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
if(connectivityManager.getActiveNetworkInfo().getState() == NetworkInfo.State.CONNECTED){
//we are connected to a network
connected = true;
}
else
connected = false;
Warning: If you are connected to a WiFi network that doesn't include internet access or requires browser-based authentication, connected will still be true.
You will need this permission in your manifest:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Void
- 898
- 1
- 11
- 16
-
NetworkInfo has been deprecated by API 29 – Arda Kazancı Dec 21 '20 at 15:33
-
Updated to getActiveNetworkInfo – Void Dec 21 '20 at 15:35
-
getActiveNetworkInfo is also deprecated. You should use the Callback with registerDefaultNetworkCallback() but I don't know if it works initially. I mean to check when opening the app that there is connection. – user7174483 Sep 10 '21 at 09:03