1

花了很多时间寻找方法(通过谷歌和这个网站),但我找不到正确的代码来写入 TextView 自己的 android ip 地址。

我尝试了很多代码,但没有一个运行。

获取android的IP地址并将其放入TextView的正确方法是什么?

谢谢。

public class MainActivity extends Activity {

private TextView textView1;
private String tag;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    textView1 =(TextView)findViewById(R.id.textView1);
    getLocalIpAddress();
    String ip = getLocalIpAddress();
    textView1.setText("IP:"+ip);
}

public 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)
    {
        Log.e(tag, ex.toString());
    }
    return "";
}   

}

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="19dp"
    android:text="TextView" />

4

2 回答 2

3
TextView tv = (TextView) findViewById(R.id.tv);
WifiManager wim= (WifiManager) getSystemService(WIFI_SERVICE);
List<WifiConfiguration> l =  wim.getConfiguredNetworks(); 
WifiConfiguration wc = l.get(0); 
tv.append("\n"+ Formatter.formatIpAddress(wim.getConnectionInfo().getIpAddress()));

AndroidManifest.xml 权限:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

或者您可以使用此代码(不使用 Formatter):

public 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)
    {
        Log.e(tag, ex.toString());
    }
    return "";
}
于 2012-11-15T21:08:53.537 回答
0

尝试这个

  String IP;
  WifiManager wim= (WifiManager) getSystemService(WIFI_SERVICE);
  List<WifiConfiguration> l =  wim.getConfiguredNetworks(); 
  WifiConfiguration wc = l.get(0);
  IP=Formatter.formatIpAddress(wim.getConnectionInfo().getIpAddress());
  tv.setText(IP);
于 2013-06-15T21:38:01.470 回答