示例#1
0
 /**
  * Check the netwoek is enable
  *
  * @param context Access to {@code ConnectivityManager} services
  * @return Available returns true, unavailable returns false
  */
 @SuppressWarnings("deprecation")
 @SuppressLint("NewApi")
 public static boolean isNetworkAvailable(Context context) {
   try {
     ConnectivityManager connectivity =
         (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
     if (connectivity == null) {
       return false;
     } else {
       if (VERSION.SDK_INT >= 21) {
         Network[] networks = connectivity.getAllNetworks();
         for (Network network : networks) {
           NetworkInfo networkInfo = connectivity.getNetworkInfo(network);
           if (networkInfo.getState() == NetworkInfo.State.CONNECTED) return true;
         }
       } else {
         NetworkInfo[] networkInfos = connectivity.getAllNetworkInfo();
         for (NetworkInfo networkInfo : networkInfos)
           if (networkInfo.getState() == NetworkInfo.State.CONNECTED) return true;
       }
     }
   } catch (Throwable e) {
     Logger.throwable(e);
   }
   return false;
 }
示例#2
0
 /**
  * Open or close the GPRS
  *
  * @param context Access to {@code ConnectivityManager} services
  * @param isEnable Open to true, close to false
  */
 public static void setGPRSEnable(Context context, boolean isEnable) {
   try {
     ConnectivityManager connectivityManager =
         (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
     Class<?> cmClass = connectivityManager.getClass();
     Class<?>[] argClasses = new Class[1];
     argClasses[0] = boolean.class;
     Method method = cmClass.getMethod("setMobileDataEnabled", argClasses);
     method.invoke(connectivityManager, isEnable);
   } catch (Throwable e) {
     Logger.throwable(e);
   }
 }
示例#3
0
 /**
  * Check the GPRS whether available
  *
  * @param context Access to {@code ConnectivityManager} services
  * @return Open return true, close returns false
  */
 public static boolean isGPRSOpen(Context context) {
   Boolean isOpen = false;
   try {
     ConnectivityManager connectivityManager =
         (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
     Class<?> cmClass = connectivityManager.getClass();
     Class<?>[] argClasses = null;
     Method method = cmClass.getMethod("getMobileDataEnabled", argClasses);
     Object[] argObject = null;
     isOpen = (Boolean) method.invoke(connectivityManager, argObject);
   } catch (Throwable e) {
     Logger.throwable(e);
   }
   return isOpen;
 }
示例#4
0
 /**
  * Tet local ip adress
  *
  * @return Such as:192.168.1.1
  */
 public static String getLocalIPAddress() {
   String ipAddress = "";
   try {
     Enumeration<NetworkInterface> netfaces = NetworkInterface.getNetworkInterfaces();
     // 遍历所用的网络接口
     while (netfaces.hasMoreElements()) {
       NetworkInterface nif = netfaces.nextElement(); // 得到每一个网络接口绑定的地址
       Enumeration<InetAddress> inetAddresses = nif.getInetAddresses();
       // 遍历每一个接口绑定的所有ip
       while (inetAddresses.hasMoreElements()) {
         InetAddress ip = inetAddresses.nextElement();
         if (!ip.isLoopbackAddress() && isIPv4Address(ip.getHostAddress())) {
           ipAddress = ip.getHostAddress();
         }
       }
     }
   } catch (Throwable e) {
     Logger.throwable(e);
   }
   return ipAddress;
 }