Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/382.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/208.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android:如何知道IP地址是Wifi IP地址?_Java_Android_Android Networking - Fatal编程技术网

Java Android:如何知道IP地址是Wifi IP地址?

Java Android:如何知道IP地址是Wifi IP地址?,java,android,android-networking,Java,Android,Android Networking,我想知道Android设备的IP地址是数据IP还是Wifi IP 1)设备首先连接到3G,现在设备将被分配到网络IP。 2) 稍后设备连接到WIFI,现在设备将被分配到WIFI IP。 3) 任何让我们知道IP地址是Wifi IP地址或网络IP的Android API 在2.3.5中使用了以下内容,一切正常,但在4.0.3中ICS存在一些问题 /** * Is the IP Address a a Wifi Ip Address. * @param ipAddr * @return

我想知道Android设备的IP地址是数据IP还是Wifi IP


1)设备首先连接到3G,现在设备将被分配到网络IP。
2) 稍后设备连接到WIFI,现在设备将被分配到WIFI IP。
3) 任何让我们知道IP地址是Wifi IP地址或网络IP的Android API

在2.3.5中使用了以下内容,一切正常,但在4.0.3中ICS存在一些问题

   /**
 * Is the IP Address a a Wifi Ip Address.
 * @param ipAddr
 * @return boolean
 */
public boolean isWifiIp(byte[] ipAddr){
    try{
        WifiManager mgr = (WifiManager)mCxt.getSystemService(Context.WIFI_SERVICE);
        int wifiIP = mgr.getConnectionInfo().getIpAddress();
        int reverseWifiIP = Integer.reverseBytes(wifiIP);  
        int byteArrayToInt = byteArrayToInt(ipAddr,0);

        if(byteArrayToInt == wifiIP || byteArrayToInt == reverseWifiIP)
            return true;
    }catch (Exception e) {
        Logger.d(TAG, e);
    }
    return false;
}


/**
 * Convert IP Address in bytes to int value.
 * @param arr
 * @param offset
 * @return int
 */
public static final int byteArrayToInt(byte[] arr, int offset) {
    if (arr == null || arr.length - offset < 4)
        return -1;

    int r0 = (arr[offset] & 0xFF) << 24;
    int r1 = (arr[offset + 1] & 0xFF) << 16;
    int r2 = (arr[offset + 2] & 0xFF) << 8;
    int r3 = arr[offset + 3] & 0xFF;
    return r0 + r1 + r2 + r3;
}

/**
 *  Fetches the IP Address of the Client. There is Delay of 2 Seconds for the API to return.
 */
public String getClientIpAddress() {
    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(isWifiIp(inetAddress.getAddress())){
                    Logger.d(TAG, "-------- Local IP Address; Not Valid: "+inetAddress.getHostAddress());
                    continue;
                }
                if (!inetAddress.isLoopbackAddress()) {
                    String ipAddress = Formatter.formatIpAddress(inetAddress.hashCode());
                    Logger.d(TAG, "-------- Some Valid IPv4 is ---"+ipAddress);
                    return ipAddress;
                }
            }
        }
    } catch (SocketException ex) {
        Logger.e(TAG, ex.toString());
    }
    return null;
}
/**
*IP地址是Wifi IP地址。
*@param-ipAddr
*@返回布尔值
*/
公共布尔值isWifiIp(字节[]ipAddr){
试一试{
WifiManager mgr=(WifiManager)mCxt.getSystemService(Context.WIFI_服务);
int wifiIP=mgr.getConnectionInfo().getIpAddress();
int reverseewifip=整数。reverseebytes(wifiIP);
int byteArrayToInt=byteArrayToInt(ipAddr,0);
if(byteArrayToInt==wifiIP | | byteArrayToInt==reverseWifiIP)
返回true;
}捕获(例外e){
d(标签e);
}
返回false;
}
/**
*将以字节为单位的IP地址转换为int值。
*@param-arr
*@param偏移量
*@return int
*/
公共静态final int byteArrayToInt(字节[]arr,int偏移量){
if(arr==null | | arr.length-偏移量<4)
返回-1;

int r0=(arr[offset]&0xFF)您无法根据IP地址检测连接类型,因为您的移动网络和家庭WiFi网络都可以分配专用IP地址

您需要做的是首先检测您是否有移动网络或WiFi连接,然后根据该信息获取该连接的IP地址。

尝试此代码

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

import android.app.Activity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.widget.Toast;

import com.blundell.tut.R;

public class MainActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        checkAvailableConnection();
    }

    void checkAvailableConnection() {
        ConnectivityManager connMgr = (ConnectivityManager) this
                .getSystemService(Context.CONNECTIVITY_SERVICE);

        final android.net.NetworkInfo wifi = connMgr
                .getNetworkInfo(ConnectivityManager.TYPE_WIFI);

        final android.net.NetworkInfo mobile = connMgr
                .getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

        if (wifi.isAvailable()) {

            WifiManager myWifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
            WifiInfo myWifiInfo = myWifiManager.getConnectionInfo();
            int ipAddress = myWifiInfo.getIpAddress();
            System.out.println("WiFi address is "
                    + android.text.format.Formatter.formatIpAddress(ipAddress));

        } else if (mobile.isAvailable()) {

            GetLocalIpAddress();
            Toast.makeText(this, "3G Available", Toast.LENGTH_LONG).show();
        } else {
            Toast.makeText(this, "No Network Available", Toast.LENGTH_LONG)
                    .show();
        }
    }

    private String GetLocalIpAddress() {
        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()) {
                        return inetAddress.getHostAddress().toString();
                    }
                }
            }
        } catch (SocketException ex) {
            return "ERROR Obtaining IP";
        }
        return "No IP Available";
    }
}
导入java.net.InetAddress;
导入java.net.NetworkInterface;
导入java.net.SocketException;
导入java.util.Enumeration;
导入android.app.Activity;
导入android.content.Context;
导入android.net.ConnectivityManager;
导入android.net.wifi.WifiInfo;
导入android.net.wifi.WifiManager;
导入android.os.Bundle;
导入android.widget.Toast;
导入com.blundell.tut.R;
公共类MainActivity扩展了活动{
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
checkAvailableConnection();
}
void checkAvailableConnection(){
ConnectivityManager connMgr=(ConnectivityManager)此
.getSystemService(Context.CONNECTIVITY\u服务);
最终android.net.NetworkInfo wifi=connMgr
.getNetworkInfo(ConnectionManager.TYPE_WIFI);
最终android.net.NetworkInfo mobile=connMgr
.getNetworkInfo(ConnectionManager.TYPE_MOBILE);
如果(wifi.isAvailable()){
WifiManager myWifiManager=(WifiManager)getSystemService(WIFI_服务);
WifiInfo myWifiInfo=myWifiManager.getConnectionInfo();
int-ipAddress=myWifiInfo.getIpAddress();
System.out.println(“WiFi地址为”
+android.text.format.Formatter.formatIpAddress(ipAddress));
}else if(mobile.isAvailable()){
GetLocalIpAddress();
Toast.makeText(此“3G可用”,Toast.LENGTH_LONG).show();
}否则{
Toast.makeText(此“无网络可用”,Toast.LENGTH\u LONG)
.show();
}
}
私有字符串GetLocalIpAddress(){
试一试{
对于(枚举en=网络接口
.getNetworkInterfaces();en.hasMoreElements();){
NetworkInterface intf=en.nextElement();
对于(枚举枚举enumIpAddr=intf
.getInetAddresses();EnumipAddress.hasMoreElements();){
InetAddress InetAddress=enumIpAddr.nextElement();
如果(!inetAddress.isLoopbackAddress()){
返回inetAddress.getHostAddress().toString();
}
}
}
}捕获(SocketException例外){
返回“获取IP时出错”;
}
返回“无可用IP”;
}
}
找到了窍门。。


getName for WiFi将从wlan开始。使用此验证WiFi IP。

为了补充此答案,请看一看关于检测Android上的网络连接类型的问题:您是否阅读了Guido García的建议链接。它提供了检测几种不同类型网络的良好说明。这个问题在很多方面都是错误的级别…错在哪里更精确,并使其正确。。