用于在 Android 应用程序中配置静态 IP 地址的 API

发布于 2024-09-02 20:54:30 字数 366 浏览 6 评论 0原文

是否可以在应用程序中设置 Android 接口的 IP 地址?

我可以使用 java.net.NetworkInterface 查询可用接口及其当前地址,但这不提供更改这些接口的工具。我是不是错过了什么地方,或者这是不允许的?

我希望能够使我的应用程序在运行时在“现成的”设备上更改或添加一个或多个现有接口的别名。 (2.1/2.2)。理想情况下,我想对 IPv4IPv6 地址。

Is it possible to set the IP address of an interface in Android within an application?

I can query the available interfaces and their current addresses using java.net.NetworkInterface, but this doesn't provide a facility to change these. Did I just miss something somewhere, or is it not allowed?

I was hoping to be able to make my application either change or add an alias to one or more of the existing interfaces at runtime on an "off the shelf" device. (2.1/2.2). Ideally I'd like to do this for both IPv4 and IPv6 addresses.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

呢古 2024-09-09 20:54:30

Settings.System 包含几个可用于此目的的标志:

  • WIFI_USE_STATIC_IP
  • WIFI_STATIC_IP
  • WIFI_STATIC_NETMASK
  • WIFI_STATIC_GATEWAY
  • WIFI_STATIC_DNS1WIFI_STATIC_DNS2

您还需要 android.permission.WRITE_SETTINGS为您的申请声明的许可。

然后在您的活动中:

final ContentResolver cr = getContentResolver();
Settings.System.putInt(cr, Settings.System.WIFI_USE_STATIC_IP, 1);
Settings.System.putString(cr, Settings.System.WIFI_STATIC_IP, "you.re.ip.addr");
// call putString() for each value to set for your interface

如果您想更改运营商的 3G/4G 等接口的 IP 地址,我认为这是不可能的 - 因为它连接到运营商并使用他们的 DHCP/安全性来使您能够连接和使用他们的服务(有点像在未经 ISP 同意的情况下更改电缆调制解调器的外部 IP)。

Settings.System includes several flags you can use for this:

  • WIFI_USE_STATIC_IP
  • WIFI_STATIC_IP
  • WIFI_STATIC_NETMASK
  • WIFI_STATIC_GATEWAY
  • WIFI_STATIC_DNS1 and WIFI_STATIC_DNS2

You'll also need the android.permission.WRITE_SETTINGS permission declared for your application.

Then in your activity:

final ContentResolver cr = getContentResolver();
Settings.System.putInt(cr, Settings.System.WIFI_USE_STATIC_IP, 1);
Settings.System.putString(cr, Settings.System.WIFI_STATIC_IP, "you.re.ip.addr");
// call putString() for each value to set for your interface

If you want to change the IP address of the carrier's 3G/4G,etc interface, I do not believe this is possible - as it is connected to the carrier and uses their DHCP/security for enabling you to connect and use their services (sort of like changing the external IP of your cable modem without the consent of your ISP).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文