Pages Navigation Menu

Coding is much easier than you think

To Check Whether Internet Connection Available in Android

NetworkInfo class provides a non-static method isAvailable() which will return null if there no internet .

getActiveNetworkInfo() method of ConnectivityManager returns a NetworkInfo instance representing the first connected network interface it can find or null if it cant find any connected network. We could find whether internet connection is available by checking this method .
** UPDATE: Android Complete tutorial now available here.
Below code snippet will explain it .

private boolean doNetworkAvailable() 
    ConnectivityManager connectivityManager 
          = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();

return activeNetworkInfo != null && activeNetworkInfo.isConnectedOrConnecting(); 

In android manifest.xml , You have give following entry for permission.

Please Read It Carefully .
Return true from  NetworkInfo doesn’t not guarantee that a particular networked service is available.
There are many other issue given below can  prevent your app from reaching a server.

Networks issues,
server downtime
low signal
captive portals
content filters


To check it is connected mobile internet or WIFI

ConnectivityManagers  getAllNetworkInfo() method returns list of  NetworkInfo object . It contains NetworkInfo object of all possible network connection.

private boolean checkNetworkConnection() 
 ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
 NetworkInfo[] netInfo = cm.getAllNetworkInfo();
 for (NetworkInfo simpleCode : netInfo) 
  if ("WIFI".equalsIgnoreCase(ni.getTypeName()))
   if (simpleCode.isConnected())
   Toast.makeText(getApplicationContext(), "Network connected through WIFI", Toast.LENGTH_SHORT).show(); 
   if ("MOBILE".equalsIgnoreCase(ni.getTypeName()))
     if (simpleCode.isConnected())
 Toast.makeText(getApplicationContext(), "Network connected through Mobile Internet", Toast.LENGTH_SHORT).show(); 

You can also use getType() (simpleCodes.getType() in previous example) instead getTypeName(), because getType() provides a finer level of granularity and might return one of TYPE_MOBILE, TYPE_WIFI, TYPE_WIMAX, TYPE_ETHERNET, TYPE_BLUETOOTH and so on .
TYPE_DUMMY – Dummy data connection.
TYPE_ETHERNET – The Default Ethernet data connection.
TYPE_MOBILE – The Default Mobile data connection.
TYPE_MOBILE_DUN – A DUN-specific Mobile data connection.
TYPE_MOBILE_HIPRI – A High Priority Mobile data connection.
TYPE_MOBILE_MMS – An MMS-specific Mobile data connection.
TYPE_MOBILE_SUPL – A SUPL-specific Mobile data connection.
TYPE_WIFI – The Default WIFI data connection.
TYPE_WIMAX – The Default WiMAX data connection.

About Gokul

I am very much interested in android and Wicket framework. A core android developer and working in android native app development and responsive web mobile design. I have also worked in wicket fame work and java web development. I will keep on updating you about android and wicket framework and answer your query.

%d bloggers like this: