public static String getIPAddress(Context context) {        NetworkInfo info = ((ConnectivityManager) context                .getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();        if (info != null && info.isConnected()) {            if (info.getType() == ConnectivityManager.TYPE_MOBILE) {//当前使用2G/3G/4G网络                try {                    //Enumeration en=NetworkInterface.getNetworkInterfaces();                    for (Enumeration en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) {                        NetworkInterface intf = en.nextElement();                        for (Enumeration enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) {                            InetAddress inetAddress = enumIpAddr.nextElement();                            if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet4Address) {                                return inetAddress.getHostAddress();                            }                        }                    }                } catch (SocketException e) {                    e.printStackTrace();                }            } else if (info.getType() == ConnectivityManager.TYPE_WIFI) {//当前使用无线网络                WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);                WifiInfo wifiInfo = wifiManager.getConnectionInfo();                String ipAddress = intIP2StringIP(wifiInfo.getIpAddress());//得到IPV4地址                return ipAddress;            }        } else {            //当前无网络连接,请在设置中打开网络        }        return null;    }    /**     * 将得到的int类型的IP转换为String类型     *     * @param ip     * @return     */    public static String intIP2StringIP(int ip) {        return (ip & 0xFF) + "." +                ((ip >> 8) & 0xFF) + "." +                ((ip >> 16) & 0xFF) + "." +                (ip >> 24 & 0xFF);    }

更多相关文章

  1. android在service中使用AsyncHttpClient加载网络资源
  2. android 网络语音电话合集 此文为备份
  3. android 网络异步加载数据进度条
  4. Android显示网络图片相关实现方法浅谈
  5. android自定义属性 format类型
  6. android 网络之 httppost
  7. GridView(网络视图)的基本使用
  8. Android进阶(一)几种网络请求方式详解

随机推荐

  1. Android体系结构分析
  2. [置顶] Android 启动 白屏问题 解决
  3. Android解析WindowManagerService(一)WMS的
  4. Android省市区3级联动的地址列表的实现
  5. android 特效之一 电视机关闭特效
  6. Android(安卓)studio开发简易音乐app遇到
  7. Android使用SAX解析XML(1)
  8. Android http中文乱码问题
  9. Android LayoutInflater的获取及inflate(
  10. android TextView控件属性列表