Check network connection and internet availability in Android


Before you perform any network operations, you must first check that are you connected to that network or internet etc.

In android we can check internet connectivity using ConnectivityManager class. We need to instantiate an object of ConnectivityManager class by calling getSystemService() method.

After this we can get information of all networks using getAllNetworkInfo() method. This method will return an array of NetworkInfo type.

First we need following permission in order to access network state. Add following permission to your AndroidManifest.xml file.

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

Following is simple snippet for network connection check

public static boolean isNetworkConnected(Context ctx) {
    ConnectivityManager cm = (ConnectivityManager) 
                                  ctx.getSystemService (Context.CONNECTIVITY_SERVICE);
    NetworkInfo ni = cm.getActiveNetworkInfo();
    return ni != null && ni.isConnectedOrConnecting();

Once you instantiate the object of ConnectivityManager class, you can use getAllNetworkInfo method to get the information of all the networks. This method returns an array of NetworkInfo. Or you can use getActiveNetworkInfo() to get info about active network.

public static int TYPE_WIFI = 1;
public static int TYPE_MOBILE = 2;
public static int TYPE_NOT_CONNECTED = 0;

public static int getConnectivityStatus(Context context) {
    ConnectivityManager cm = (ConnectivityManager) 
                                  ctx.getSystemService (Context.CONNECTIVITY_SERVICE);

    NetworkInfo ni = cm.getActiveNetworkInfo();
    if (null != ni) {
        if(ni.getType() == ConnectivityManager.TYPE_WIFI)
            return TYPE_WIFI;

        if(ni.getType() == ConnectivityManager.TYPE_MOBILE)
            return TYPE_MOBILE;

Following method checks if device is connected to internet (there is a possibility it's connected to a network but not to internet).

public static boolean isInternetAvailable() {
    String host = "";
    int port = 80;
    Socket socket = new Socket();

    try {
        socket.connect(new InetSocketAddress(host, port), 2000);
        return true;
    } catch (IOException e) {
        try {
        } catch (IOException es) {}
        return false;

Example of MainActivity which shows result for network connection and internet available

public class MainActivity extends AppCompatActivity {
    TextView tvN, tvI;

    private class TaskIsInternetAvailable extends AsyncTask<String, Void, Boolean> {
        protected Boolean doInBackground(String... args) {
            Boolean result = NetworkUtil.isInternetAvailable();
            return result;

        protected void onPostExecute(Boolean result) {
            Toast.makeText(MainActivity.this, String.valueOf(result), Toast.LENGTH_SHORT).show();
            tvI.setText("Internet connected");

    protected void onCreate(Bundle savedInstanceState) {

        tvN = (TextView) findViewById(;
        tvI = (TextView) findViewById(;

        if (NetworkUtil.isNetworkConnected(this)) {
            tvN.setText("Network connected");

            TaskIsInternetAvailable task = new TaskIsInternetAvailable();

Broadcast Receiver to handle changes in network state

You can easily handle the changes in network state by creating your own Broadcast Receiver. Following is a broadcast receiver class where we handle the changes in network.

Check onReceive() method. This method will be called when state of network changes. Here we are just creating a Toast message and displaying current network state. You can write your custom code in here to handle changes in connection state.

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class NetworkChangeReceiver extends BroadcastReceiver {
    public void onReceive(final Context context, final Intent intent) {
        int status = NetworkUtil.getConnectivityStatus(context);
        Toast.makeText(context, "STATUS: " + String.valueOf(status), Toast.LENGTH_LONG).show();

Once we define our BroadcastReceiver, we need to define the same in AndroidMenifest.xml file. Add following to your menifest file.

<application  ...>
                <action android:name="" />
                <action android:name="" />

We defined our broadcast receiver class in menifest file. Also we defined two intent CONNECTIVITY_CHANGE and WIFI_STATE_CHANGED. Thus this will register our receiver for given intents. Whenever there is change in network state, android will fire these intents and our broadcast receiver will be called.

comments powered by Disqus