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

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 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) {//获取IPv4的IP地址
                             return inetAddress.getHostAddress();
                         }
                     }
                 }
             } catch (SocketException e) {
                 e.printStackTrace();
             }


            return null;  
        }  

你可能感兴趣的:(android)