daily pastebin goal
84%
SHARE
TWEET

Android Network Connection Checker

a guest Jul 12th, 2011 96 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class Network {
  2.  
  3.         public static final int CONNECTION_NONE = 0;
  4.         public static final int CONNECTION_WIFI = 1;
  5.         public static final int CONNECTION_MOBILE = 2;
  6.  
  7.         /**
  8.         * If you use this method then you must add this line to your manifest
  9.         * <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
  10.         *
  11.         */
  12.        
  13.         public static int hasNetworkConnection(Context context) {
  14.                 int returnValue = CONNECTION_NONE;
  15.                 final ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
  16.  
  17.                 if (null != cm) {
  18.                         final NetworkInfo [] netInfo = cm.getAllNetworkInfo();
  19.                         for (NetworkInfo ni : netInfo) {
  20.                                 if (ni.isConnected()) {
  21.                                         if ("WIFI".equals(ni.getTypeName())) {
  22.                                                 returnValue |= CONNECTION_WIFI;
  23.                                                 continue;
  24.                                         }
  25.  
  26.                                         if ("MOBILE".equals(ni.getTypeName())) {
  27.                                                 returnValue |= CONNECTION_MOBILE;
  28.                                                 continue;
  29.                                         }
  30.                                 }
  31.                         }
  32.                 }
  33.                 return returnValue;
  34.         }
  35. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top