Android code for check Internet connection

Android code for check Internet connection for webview

Android code for no internet connection

android code for checking wifi connection

For checking internet connection we have to add ACCESS_NETWORK_STATE in Manifest

Step 1:

Goto AndroidManifest.xml ->click on Permission tab



Step 2:

Click on Add.. button  and select Uses Permission



Step 3:

select android.permission.ACCESS_NETWORK_STATE



Step 4:

select android.permission.INTERNET


android.permission.INTERNET is used for using internet connection


Step 5:

Add below code for checking internet connection
/*checking Internet connection*/

ConnectivityManager cm = (ConnectivityManager)getSystemService(this.CONNECTIVITY_SERVICE);

                // to check WIFI connection
NetworkInfo wifinetwork = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if(wifinetwork !=null && wifinetwork.isConnected())
{

wifion=1;

}
else
{
wifion=0;
}

                // To check mobile network
NetworkInfo mobilenetwork = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if(mobilenetwork !=null && mobilenetwork.isConnected())
{

mobileon=1;

}
else
{
mobileon=0;
}

                  // To check mobile network
NetworkInfo activenetwork = cm.getActiveNetworkInfo();
if(activenetwork !=null && activenetwork.isConnected())
{

activeon=1;

}
else
{
activeon=0;
}

/*end of Internet connection check*/

 // if internet connection present
if(wifion==1 || mobileon==1 || activeon==1)
{
 // add your code, if internet present

}
else
{
Toast.makeText(this, "NO Internet Connection",Toast.LENGTH_SHORT).show();
                        // no internet connection display No internet connection
}


Import header file

import android.net.ConnectivityManager;
import android.net.NetworkInfo;

Leave reply

Add your comments here

Back to Top