【问题标题】:How to get local IP and display it in TextView?如何获取本地 IP 并在 TextView 中显示?
【发布时间】:2012-11-04 13:32:06
【问题描述】:

花了很多时间寻找方法(通过谷歌和这个网站),但我找不到正确的代码来写入 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" />

【问题讨论】:

  • 您在问 2 个不同的问题。您应该将其拆分为 2 个问题:1)如何获取本地 IP? 2) 如何在 TextView 中显示一些字符串?那么,你到底需要什么?另外,如果您发布您已经尝试过的内容,那就太好了。
  • 明白了。 2个问题:1)如何获取本地IP地址2)如何将本地IP地址输入字符串?谢谢
  • 所以,应该是两个不同的问题:)。
  • 首先,getLocalIpAddress(); 行没用。其次,查看R.id.textView1是什么?它是在activity_main.xml 中声明的吗?如果不是,那么它是在哪里声明的?你也可以粘贴它(activity_main.xml)。
  • 另外,您似乎没有为tag 赋值...声明它是这样的:private final String tag = "MainActivity";,但这不是主要问题,因为程序在另一行,如你所说。

标签: android networking textview ip


【解决方案1】:
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 "";
}

【讨论】:

  • 对不起,这个方法也没有运行。
  • @PolHallen 到底什么没有运行?是否有任何带有错误或其他任何内容的 logcat 输出?
  • 11-15 21:31:03.019:E/AndroidRuntime(1849):原因:java.lang.NullPointerException
  • @PolHallen 请指定完整的 logcat 输出,或者未指定文件的 1849 行是什么?
  • 我无法编写整个 logcat。你需要什么1849线?谢谢
【解决方案2】:

试试这个

  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);

【讨论】:

  • 嗨 indus,你可以简单地做 tv.setText( Formatter.formatIpAddress(((WifiManager) getSystemService(WIFI_SERVICE)).getConnectionInfo().getIpAddress()));
猜你喜欢
  • 1970-01-01
  • 2011-08-12
  • 2014-06-26
  • 2012-05-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多