首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何获取本端IP并在TextView中展示?

如何获取本端IP并在TextView中展示?
EN

Stack Overflow用户
提问于 2012-11-15 21:05:02
回答 2查看 8.6K关注 0票数 1

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

我试了很多很多代码,但是没有人运行。

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

谢谢。

代码语言:javascript
运行
AI代码解释
复制
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 "";
}   

}

代码语言:javascript
运行
AI代码解释
复制
<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" />

EN

回答 2

Stack Overflow用户

发布于 2012-11-15 21:08:54

代码语言:javascript
运行
AI代码解释
复制
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权限:

代码语言:javascript
运行
AI代码解释
复制
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

或者,您可以使用以下代码(不使用格式化程序):

代码语言:javascript
运行
AI代码解释
复制
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 "";
}
票数 3
EN

Stack Overflow用户

发布于 2013-06-15 21:38:01

尝尝这个

代码语言:javascript
运行
AI代码解释
复制
  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);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13406180

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档