Monday, January 31, 2011

To Check internet Connection status/Type in Android




Aim: To Check internet Connection status/Type in Android.
Here is some example code to check the network status .


void chkConnectionStatus()
{
  ConnectivityManager connMgr = (ConnectivityManager)
this.getSystemService(Context.CONNECTIVITY_SERVICE);


final android.net.NetworkInfo wifi =
connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);


final android.net.NetworkInfo mobile =
connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);


if( wifi.isAvailable() ){
Toast.makeText(this, "Wifi" , Toast.LENGTH_LONG).show();
}
else if( mobile.isAvailable() ){
Toast.makeText(this, "Mobile 3G " , Toast.LENGTH_LONG).show();
}
else
{Toast.makeText(this, "No Network " , Toast.LENGTH_LONG).show();}
}

This snippet requires the following permissions in AndroidManifest.xml file.


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



Please let me know if any.

Android Developers Blog

Ram's shared items

Amazon Product Ads