Friday, December 15, 2017

Check Internet Connection in Android Exmaple

public static boolean checkMobileInternetConn(Context _context) {
   //Create object for ConnectivityManager class which returns network related info 
 ConnectivityManager connectivity = (ConnectivityManager) _context
         .getSystemService(Context.CONNECTIVITY_SERVICE);
   //If connectivity object is not null   if (connectivity != null) {
      //Get network info - WIFI internet access 
 NetworkInfo info = connectivity.getNetworkInfo(ConnectivityManager.TYPE_WIFI) ;
       NetworkInfo info1= connectivity.getNetworkInfo(ConnectivityManager.TYPE_MOBILE) ;

      if (info != null || info1!=null) {
         //Look for whether device is currently connected to WIFI network 
 if (info.isConnected() || info1.isConnected()) {
            return true;
         }
      }
   }
   return false;
}

No comments:

Post a Comment

Featured Post

What is JavaScript? What is the role of JavaScript engine?

  The JavaScript is a Programming language that is used for converting static web pages to interactive and dynamic web pages. A JavaScript e...