Android获取当前网络状态

/** * @Title: getNetWorkState *  * @Description: 获取当前网络状态 *  * @param context * @return int */public static int getNetWorkState(Context context) {final int network_none = -1;// 没有连接网络final int network_mobile = 0;// 移动网络final int network_wifi = 1;// 无线网络// 得到连接管理器对象ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();if (activeNetworkInfo != null && activeNetworkInfo.isConnected()) {if (activeNetworkInfo.getType() == (ConnectivityManager.TYPE_WIFI)) {return network_wifi;} else if (activeNetworkInfo.getType() == (ConnectivityManager.TYPE_MOBILE)) {return network_mobile;}} else {return network_none;}return network_none;}

Android获取当前设备网络ip地址

/** * @Title: getIpAddress *   * @Description: 获取设备ip地址 *   * @return String */public static String getIpAddress() {try {for (Enumeration enNetI = NetworkInterface.getNetworkInterfaces(); enNetI.hasMoreElements();) {NetworkInterface netI = enNetI.nextElement();for (Enumeration enumIpAddr = netI.getInetAddresses();enumIpAddr.hasMoreElements();) {InetAddress inetAddress = enumIpAddr.nextElement();if (inetAddress instanceof Inet4Address &&!inetAddress.isLoopbackAddress()) {return inetAddress.getHostAddress();}}}} catch (SocketException e) {e.printStackTrace();}return "";}

 

 

更多相关文章

  1. 【阿里云镜像】切换阿里巴巴开源镜像站镜像——Debian镜像
  2. Android(安卓)经典小技巧总结
  3. android TextUtils的使用
  4. network: android 网络判断
  5. 【Android】 Android中对于时间的转换
  6. android 设置系统屏幕亮度
  7. android 查看 当前activity
  8. 【Android】Fragment将废弃onActivityCreated
  9. Android(安卓)Instrumentation自动化测试框架

随机推荐

  1. android 如何拖动控件的实现
  2. android 写入data/data/包名/file/中
  3. android boot
  4. Android 使用Handler模仿流行的AsyncHttp
  5. android googlemap的location报空指针解
  6. android技术博客汇总
  7. 如何使用 Android Studio 的 git hub 功
  8. Android(安卓)用GridView做横向滚动
  9. Android debug :arm-linux-androideabi-a
  10. How to create a custom notification on