##帮助类PhoneNetStateUtil

package com.funs.PhoneIPAddress.utils;

/**
     * 手机联网状态工具类 需要的权限 WIFI时:</br>
     * android.permission.ACCESS_WIFI_STATE</br>
     * android.permission.CHANGE_WIFI_STATE </br>
     * android.permission.WAKE_LOCK </br>
     * 手机联网时:</br>
     * android.permission.INTERNET
     */
    public class PhoneNetStateUtil {
        /** 手机网络连接状态管理者 */
        private static ConnectivityManager mConnectivityManager;
        /** 手机网络信息 */
        private static NetworkInfo mNetworkInfo;
        private static Context mContext;
    
        private static void init(Context context) {
            mContext = context;
            mConnectivityManager = (ConnectivityManager) mContext
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
        }
        /**
         * 判断网络是否可用</br>
         * 必须首先调用该方法,判断网络是否可用。可用再判断是否为WIFI
         */
        public static boolean isNetOk(Context context){
            mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            if (mConnectivityManager == null) {//判断网络连通管理器是否存在
                return false;
            }
            mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
            if (mNetworkInfo == null) {//判断网络连接信息是否存在
                return false;
            }
            if (mNetworkInfo.isConnected()) {//判断网络是否连接成功
                return true;
            }
            return false;
        }
    
        /**
         * 判断网络状态是否为WIFI联网
         */
        public static boolean isWIFI(Context context) {
            init(context);
            return (ConnectivityManager.TYPE_WIFI == mNetworkInfo.getType()) ? true
                    : false;
        }
    
        /**
         * 手机连接移动网络时,获取IP地址
         */
        public static String getMobileIpAddress(Context context){
    //        String mobileIp = "";
            StringBuilder mobileIp = new StringBuilder("");
            try {
                for (Enumeration<NetworkInterface> en = NetworkInterface
                        .getNetworkInterfaces(); en.hasMoreElements();) {
                    NetworkInterface intf = en.nextElement();
                    for (Enumeration<InetAddress> enumIpAddr = intf
                            .getInetAddresses(); enumIpAddr.hasMoreElements();) {
                        InetAddress inetAddress = enumIpAddr.nextElement();
                        if (!inetAddress.isLoopbackAddress()) {
    //                        mobileIp = inetAddress.getHostAddress().toString();
    //                        return mobileIp;
                            mobileIp.append(inetAddress.getHostAddress().toString());
                        }
                    }
    
                }
            } catch (Exception e) {
                Log.e("WifiPreference IpAddress", e.toString());
            }
            if ("".equals(mobileIp)) {
                Toast.makeText(context, "网络连接异常哦亲!", Toast.LENGTH_SHORT).show();
            }
            return mobileIp.toString();
        }
    
        /**
         * 获取本地IP地址,包含了各路由的地址,
         */
        public static String getLocalIpAddress() {
            StringBuilder sbIpAddress = new StringBuilder("");
            try {
                Enumeration<NetworkInterface> en = NetworkInterface
                        .getNetworkInterfaces();
                int one = 0;
                int two = 0;
                while (en.hasMoreElements()) {
                    NetworkInterface ni = en.nextElement();
                    one++;
                    Enumeration<InetAddress> enIp = ni.getInetAddresses();
                    while (enIp.hasMoreElements()) {
                        InetAddress inet = enIp.nextElement();
                        two++;
                        if (!inet.isLoopbackAddress()
                                && (inet instanceof Inet4Address)) {
    //                        return inet.getHostAddress().toString();
                            sbIpAddress.append("/" + inet.getHostAddress().toString());
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
    
            return sbIpAddress.toString();
        }
    
        /**
         * 手机wifi联网时,获取内网IP地址
         */
        public static String getWifiInsideIpAddress(Context context){
            init(context);
            WifiManager wifiManager = (WifiManager) mContext
                    .getSystemService(Context.WIFI_SERVICE);
            if (!wifiManager.isWifiEnabled()) {
                wifiManager.setWifiEnabled(true);
            }
            WifiInfo wifiInfo = wifiManager.getConnectionInfo();
            int ipAddress = wifiInfo.getIpAddress();
            String insideIp = int2ip(ipAddress);
            return insideIp;
        }
        /**
         * 手机wifi联网时,获取外网IP地址</br>
         * <b>因为该方法需要访问网络数据,属于耗时操作,所以应放到非UI线程中执行</b>
         */
        public static String getWifiOutsideIpAddress() {
            String outsideIp = "";
            try {
                String address = "http://ip.taobao.com/service/getIpInfo2.php?ip=myip";
                URL url = new URL(address);
                HttpURLConnection connection = (HttpURLConnection) url
                        .openConnection();
                connection.setUseCaches(false);
    
                if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
                    InputStream in = connection.getInputStream();
    
                    // 将流转化为字符串
                    BufferedReader reader = new BufferedReader(
                            new InputStreamReader(in));
    
                    String tmpString = "";
                    StringBuilder retJSON = new StringBuilder();
                    while ((tmpString = reader.readLine()) != null) {
                        retJSON.append(tmpString + "\n");
                    }
    
                    JSONObject jsonObject = new JSONObject(retJSON.toString());
                    String code = jsonObject.getString("code");
                    if (code.equals("0")) {
                        JSONObject data = jsonObject.getJSONObject("data");
                        outsideIp = data.getString("ip");
    
                    } else {
                        outsideIp = "";
                        Log.e("提示", "IP接口异常,无法获取IP地址!");
                    }
                } else {
                    outsideIp = "";
                    Log.e("提示", "网络连接异常,无法获取IP地址!");
                }
            } catch (Exception e) {
                outsideIp = "";
                Log.e("提示", "获取IP地址时出现异常,异常信息是:" + e.toString());
            }
            return outsideIp;
        }
        /**
         * 将int型数据转化为ip地址
         * @param value
         */
        private static String int2ip(int value) {
            return (value & 0xFF) + "." + ((value >> 8) & 0xFF) + "."
                    + ((value >> 16) & 0xFF) + "." + ((value >> 24) & 0xFF);
        }
    }

Android-------------获取手机IP地址的更多相关文章

  1. Xamarin.Android 获取手机IP地址

    命名空间: using System.Net; 代码: IPAddress[] address = Dns.GetHostAddresses(Dns.GetHostName()); ] != null ...

  2. Android 获取手机Mac地址,手机名称

    /** * 获取手机mac地址<br/> * 错误返回12个0 */ public static String getMacAddress(Context context) { // 获取 ...

  3. Android 获取当前IP地址

    首先设置用户权限 <uses-permission android:name="android.permission.INTERNET"></uses-permi ...

  4. 微信小程序开发(八)获取手机ip地址

    // succ.wxml <view>手机IP:{{motto.query}}</view> // succ.js var app = getApp() Page({ data ...

  5. Android 获得本地IP地址、外网IP地址、本设备网络状态信息、本地Mac地址

    本地内网IP和外网IP的区别: 根据我的经验一台电脑需要两个ip才可以上网,一个是本地的内网ip 一个是外网的ip 本地的ip 一般是192.168.1.2这种样子  只要在不同的路由器上可以重复 外 ...

  6. android获取手机信息大全

    IMEI号,IESI号,手机型号: private void getInfo() { TelephonyManager mTm = (TelephonyManager) getSystemServic ...

  7. android获取手机信息2

    IMEI号,IESI号,手机型号: private void getInfo() { TelephonyManager mTm = (TelephonyManager) getSystemServic ...

  8. [转] android获取手机信息大全

    原文链接:http://blog.csdn.net/hytfly/article/details/8552483 IMEI号,IESI号,手机型号: private void getInfo() { ...

  9. 在Thinkphp3.2.3框架下实现自动获取客户端IP地址的get_client_ip()函数

    在Thinkphp框架下使用get_client_ip()函数获取客户端IP地址十分方便: 一行代码便可以实现:$ip = get_client_ip(); 但当我们测试时会遇到后台获取的IP地址显示 ...

随机推荐

  1. Fiddler使用笔记

    http://www.cnblogs.com/TankXiao/archive/2012/02/06/2337728.html#basic   1.filter的使用,跟踪某个网站的访问,例如:hr. ...

  2. Segment对象

    Segment对象是一个有起点和终点的“线“,也就是说Segement只有两个点,至于两点之间的线是直的,还是曲的,需要其余的参数定义. 所以Segment是由起点,终点和参数三个方面决定的.Segm ...

  3. C语言基础学习基本数据类型-变量和常量

    变量和常量 什么是变量和常量?有些数据在程序运行前就预先设定,并在运行过程中不发生变化,称之为常量:有些数据在程序运行中可能发生变化或被赋值,称之为变量. 使用变量前必须先声明一个变量.变量定义的语法 ...

  4. iOS开发中文件的上传和下载功能的基本实现-备用

    感谢大神分享 这篇文章主要介绍了iOS开发中文件的上传和下载功能的基本实现,并且下载方面讲到了大文件的多线程断点下载,需要的朋友可以参考下 文件的上传 说明:文件上传使用的时POST请求,通常把要上传 ...

  5. 修改UITextField Placeholder的颜色

    修改UITextField Placeholder的颜色 1 第一种情况只是修改颜色等文字属性 创建属性字典 NSDictionary *attrsDic = @{ NSForegroundColor ...

  6. qt 拖拽 修改大小(使用了nativeEvent和winEvent)

    http://www.cnblogs.com/swarmbees/p/5621543.html http://blog.sina.com.cn/s/blog_9e59cf590102w3r6.html

  7. mysql if对数据进行处理 having对数据进行查询 thinkphp中的exp支持更复杂的where查询

    很多时候,数据库获取的信息并不是我们最终想要的,需要通过if进行处理. where支持查询 having支持后查询(查询后的数据,再筛选) 代码如下: if ($this->_post('dos ...

  8. 2015第22周六Java反射、泛型、容器简介

    Java的反射非常强大,传递class, 可以动态的生成该类.取得这个类的所有信息,包括里面的属性.方法以及构造函数等,甚至可以取得其父类或父接口里面的内容. obj.getClass().getDe ...

  9. joomla学习网站资料

    http://joomlaol.com/joomla-chinese/joomla-study/340-joomla-skills.html joomla的强大相信学过和用过joomla的童鞋都知道吧 ...

  10. ZZY的困惑

    Description ZZY有很多爱好~~比如足球.电影.三国杀.A题,而他希望在这些爱好中能收获一些东西~~但是并不是所有爱好对所有目标都是起积极作用的..ZZY十分的困惑..于是列了下自己想获得 ...