获取android手机当前网络链接IP的方法

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_31028313/article/details/70237558

1、所需权限

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>  
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>  
<uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission> 
<uses-permission android:name="android.permission.INTERNET"></uses-permission> 

2、代码

/**
     * 获取IP
     * @param context
     * @return
     */
     public static String getIp(final Context context) {
         String ip = null;
            ConnectivityManager conMan = (ConnectivityManager) context
                    .getSystemService(Context.CONNECTIVITY_SERVICE);

            // mobile 3G Data Network
            android.net.NetworkInfo.State mobile = conMan.getNetworkInfo(
                    ConnectivityManager.TYPE_MOBILE).getState();
            // wifi
            android.net.NetworkInfo.State wifi = conMan.getNetworkInfo(
                    ConnectivityManager.TYPE_WIFI).getState();

            // 如果3G网络和wifi网络都未连接,且不是处于正在连接状态 则进入Network Setting界面 由用户配置网络连接
            if (mobile == android.net.NetworkInfo.State.CONNECTED
                    || mobile == android.net.NetworkInfo.State.CONNECTING) {
                ip =  getLocalIpAddress();
            }
            if (wifi == android.net.NetworkInfo.State.CONNECTED
                    || wifi == android.net.NetworkInfo.State.CONNECTING) {
                  //获取wifi服务  
                WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);  
                //判断wifi是否开启  
                if (!wifiManager.isWifiEnabled()) {  
                wifiManager.setWifiEnabled(true);    
                }  
                WifiInfo wifiInfo = wifiManager.getConnectionInfo();       
                int ipAddress = wifiInfo.getIpAddress();   
                ip =(ipAddress & 0xFF ) + "." +       
                        ((ipAddress >> 8 ) & 0xFF) + "." +       
                        ((ipAddress >> 16 ) & 0xFF) + "." +       
                        ( ipAddress >> 24 & 0xFF) ;    
            }
            return ip;

        }

     /**
      * 
      * @return 手机GPRS网络的IP
      */
        private static String getLocalIpAddress()  
        {  
             try {
                 //Enumeration<NetworkInterface> en=NetworkInterface.getNetworkInterfaces();
                 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() && inetAddress instanceof Inet4Address) {//获取IPv4的IP地址
                             return inetAddress.getHostAddress();
                         }
                     }
                 }
             } catch (SocketException e) {
                 e.printStackTrace();
             }


            return null;  
        }  
展开阅读全文

没有更多推荐了,返回首页