I want to execute my application offline also, so I need to check if currently an internet connection is available or not. Can anybody tell me how to check if internet is available or not in android? Give sample code. I tried with the code below and checked using an emulator but it's not working
public boolean isInternetConnection() { ConnectivityManager connectivityManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE); return connectivityManager.getActiveNetworkInfo().isConnectedOrConnecting(); }
Thanks
Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − To find the internet status we have to add network state permission to AndroidManifest. xml file as shown below.
The getActiveNetworkInfo() method of ConnectivityManager returns a NetworkInfo instance representing the first connected network interface it can find or null if none of the interfaces are connected. Checking if this method returns null should be enough to tell if an internet connection is available or not.
To check the Wi-Fi statusOn the home screen, tap Apps > Settings. Under Network Connections, tap Wi-Fi; then tap the connected Wi-Fi network. Check the Signal strength.
In android, we can determine the internet connection status easily by using getActiveNetworkInfo() method of ConnectivityManager object. Following is the code snippet of using the ConnectivityManager class to know whether the internet connection is available or not.
This will tell if you're connected to a network:
boolean connected = false; ConnectivityManager connectivityManager = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE); if(connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState() == NetworkInfo.State.CONNECTED || connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).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" />
You can use two method :
1 - for check connection :
private boolean isNetworkConnected() { ConnectivityManager cm = (ConnectivityManager) getContext().getSystemService(Context.CONNECTIVITY_SERVICE); return cm.getActiveNetworkInfo() != null; }
2 - for check internet :
public boolean internetIsConnected() { try { String command = "ping -c 1 google.com"; return (Runtime.getRuntime().exec(command).waitFor() == 0); } catch (Exception e) { return false; } }
Add permissions to 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" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With