【问题标题】:How Can I Get My Dynamic IP Address of My Android Device如何获取我的 Android 设备的动态 IP 地址
【发布时间】:2023-03-04 16:39:01
【问题描述】:

我在 android manifest 文件中授予访问 Internet 的权限,如下所示。 <uses-permission android:name="android.permission.INTERNET" />

以下代码将以简单的应用程序编写。

package com.GetIP;


import java.net.InetAddress;
import android.app.Activity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class IPAddress extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Button b=(Button)findViewById(R.id.btn1);

    b.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {

            // TODO Auto-generated method stub
             try
                {
                    InetAddress ownIP=InetAddress.getLocalHost();
                    //System.out.println("IP of my Android := "+ownIP.getHostAddress());

                    Toast.makeText(getApplicationContext(), ownIP.toString(), Toast.LENGTH_LONG).show();
                }
                catch (Exception e)
                {
                    System.out.println("Exception caught ="+e.getMessage());
                    }
        }
    });
}

}

上面的代码输出为 localhost/127.0.0.1,但它是默认 IP 地址,但我希望我的设备的动态 IP 地址用于聊天应用程序。

【问题讨论】:

    标签: android dynamic ip-address


    【解决方案1】:

    您可以使用以下代码获取附加到设备的 IP 地址列表。

    public static List<String> getLocalIpAddress() {
        List<String> list = new ArrayList<String>();
        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()) {
                        list.add(inetAddress.getHostAddress().toString());
                    }
                }
            }
        } catch (SocketException ex) {
            Log.e(TAG, ex.toString());
        }
        return list;
    }
    

    【讨论】:

      【解决方案2】:

      这段代码会给你你的本地IP地址:

      public static String getLocalIpAddressString() {
          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 null;
      }
      

      【讨论】:

      • 这段代码会给我 IPv6 地址,但我希望它是 IPv4 地址。请建议我将 IPv6 转换为 IPv4 地址的 java 代码。
      • @jay 你有同样的解决方案吗,如果你有请分享。
      • InetAddressUtils.isIPv4Address 可能有用吗?
      • @pyus13:我没有得到任何解决方案,但我改变了实现方式。
      猜你喜欢
      • 1970-01-01
      • 2013-12-14
      • 1970-01-01
      • 1970-01-01
      • 2013-06-29
      • 2015-02-26
      • 2021-04-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多