Pages

Ads 468x60px

Monday, August 19, 2013

Android: Verify Device Internet Connection


This code verifies either device is connected to network or not.


// Verify Network Connection
protected boolean isNetworkConnected(Activity context) {
boolean connectedWifi = false; // connected to WiFi
   boolean connectedMobile = false; // connected to Mobile

   ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
   NetworkInfo[] netInfo = connectivityManager.getAllNetworkInfo();
   for (NetworkInfo ni : netInfo) {
       if (ni.getTypeName().equalsIgnoreCase("WIFI"))
           if (ni.isConnected())
            connectedWifi = true;
       if (ni.getTypeName().equalsIgnoreCase("MOBILE"))
           if (ni.isConnected())
            connectedMobile = true;
   }
   return connectedWifi || connectedMobile;
}

No comments:

Post a Comment