用于在 Android 应用程序中配置静态 IP 地址的 API [英] API for configuring static IP addresses in an Android application
问题描述
是否可以在应用程序中设置 Android 中某个接口的 IP 地址?
Is it possible to set the IP address of an interface in Android within an application?
我可以使用 java.net.NetworkInterface
查询可用接口及其当前地址,但这不提供更改这些的工具.是我在某处错过了什么,还是不允许的?
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?
我希望能够让我的应用程序在运行时在现成"设备上更改或添加一个或多个现有接口的别名.(2.1/2.2).理想情况下,我想为 IPv4 和 IPv6 地址.
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.
推荐答案
Settings.系统
包括几个可以用于此目的的标志:
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
和WIFI_STATIC_DNS2
您还需要 android.permission.WRITE_SETTINGS
为您的应用程序声明的权限.
You'll also need the android.permission.WRITE_SETTINGS
permission declared for your application.
然后在您的活动中:
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).
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).
这篇关于用于在 Android 应用程序中配置静态 IP 地址的 API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!