在Android应用程序配置静态IP地址的API [英] API for configuring static IP addresses in an Android application

查看:1070
本文介绍了在Android应用程序配置静态IP地址的API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能在应用程序中设置的Andr​​oid接口的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.

推荐答案

<一个href="http://developer.android.com/reference/android/provider/Settings.System.html"><$c$c>Settings.System包括一些标志,你可以使用这个:

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
  • WIFI_USE_STATIC_IP
  • WIFI_STATIC_IP
  • WIFI_STATIC_NETMASK
  • WIFI_STATIC_GATEWAY
  • WIFI_STATIC_DNS1 and WIFI_STATIC_DNS2

您还需要在<一个href="http://developer.android.com/reference/android/Manifest.permission.html#WRITE_SETTINGS"><$c$c>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 /安全性,使您能够连接并使用他们的服务(有点像改变你的电缆调制解调器的外部IP没有你的ISP的同意)。

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆