Android How to check network status(Both Wifi and Mobile 3G)

There is always a need for a developer to check the network status of the network connected to the phone. Here are some example code to check the network status .

You need the following permission to get status

[sourcecode language="xml"]
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
[/sourcecode]

[sourcecode language="java"]
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.widget.Toast;
public class pingtest extends Activity {
 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.main);

 chkStatus();

 }

 void chkStatus()
 {
 final 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();}
 }

 }

[/sourcecode]

Android How to check network status mobile 3G Android How to check network status Wifi