Check Internet Connectivity

DOWNLOAD CODE

Related Examples

In this example we will see how to check internet connectivity.
When we have requirement to call file on server or call webservice then first check internet connctivity.
If internet connection is showing connected then we will call file on server or call webservice.
Required ACCESS_NETWORK_STATE permission in mainfest.xml file.

File : AndroidMainifest.xml

 


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.androidexample.internetcheck"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="8" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name="com.androidexample.internetcheck.InternetCheck"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
</manifest>

 

File : src/com.androidexample.internetcheck/InternetCheck.java

 

Check network state.

 


import android.app.Activity;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class InternetCheck extends Activity {
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        final Button Save = (Button) findViewById(R.id.save);
        
        Save.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				isInternetOn();
			}
        });	
    }
    
    public final boolean isInternetOn() {
    	
    	// get Connectivity Manager object to check connection
		ConnectivityManager connec =  
                       (ConnectivityManager)getSystemService(getBaseContext().CONNECTIVITY_SERVICE);
		
		   // Check for network connections
			if ( connec.getNetworkInfo(0).getState() == android.net.NetworkInfo.State.CONNECTED ||
			     connec.getNetworkInfo(0).getState() == android.net.NetworkInfo.State.CONNECTING ||
			     connec.getNetworkInfo(1).getState() == android.net.NetworkInfo.State.CONNECTING ||
			     connec.getNetworkInfo(1).getState() == android.net.NetworkInfo.State.CONNECTED ) {
			   
				// if connected with internet
				
			    Toast.makeText(this, " Connected ", Toast.LENGTH_LONG).show();
			    return true;
			    
			} else if ( 
              connec.getNetworkInfo(0).getState() == android.net.NetworkInfo.State.DISCONNECTED ||
              connec.getNetworkInfo(1).getState() == android.net.NetworkInfo.State.DISCONNECTED  ) {
			  
				Toast.makeText(this, " Not Connected ", Toast.LENGTH_LONG).show();
			    return false;
			}
		  return false;
		}
}