Breaking News
Loading...

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

8:01 PM

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




0 komentar:

Post a Comment

 
Toggle Footer