Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check currently internet connection is available or not in android

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

like image 625
mohan Avatar asked Mar 29 '11 14:03

mohan


People also ask

How do I know if my Android is connected to the internet?

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.

How do I know if my internet connection is available?

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.

How do I check Wi-Fi status on Android?

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.

How do you check internet is on or off in Android programmatically?

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.


2 Answers

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" /> 
like image 200
james Avatar answered Oct 11 '22 14:10

james


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" /> 
like image 21
Masoud Siahkali Avatar answered Oct 11 '22 16:10

Masoud Siahkali