Sep 12, 2011

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

public static boolean checkNetworkStatus(Context context) {

ConnectivityManager connectivty = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE);

TelephonyManager telephony = (TelephonyManager) context .getSystemService(Context.TELEPHONY_SERVICE);

NetworkStatus netStatus = new NetworkStatus(connectivty, telephony);

if (netStatus.isNetworkAvailable() == true) {

Log.e(" in checkNetworkStatus()", "network avalible");

return true;

} else {

Log.e(" in checkNetworkStatus()", "no network");

return false;

}

}


wifi-

----

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();} 
 }   }




2 comments:

  1. I want to check my phone network status i mean the gsm and cdma network status i have been searching a code for it but it going in vain hope this blog helps me

    ReplyDelete