分配静态IP地址在Android 3.x和4.x Wifi网络 [英] Assign static IP address for Wifi network on Android 3.x and 4.x

查看:154
本文介绍了分配静态IP地址在Android 3.x和4.x Wifi网络的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

林工作的一个项目,并应设置为无线静态IP地址(DNS,子网掩码,网关),如果用户希望它的功能。

Im working on one project and there should be a functionality for setting static IP address (DNS, Netmask, Gateway) for Wifi if user want it.

我最初和实际的解决方案 android.provider.Settings.System 类的使用,使此功能,但这种解决方案成功只能为Android 2.x设备。

My initial and actual solution is an usage of android.provider.Settings.System class that allows this feature but this solution works successfully only for Android 2.x devices.

这是很好的,肯定我不是完全为零,但它会很高兴得到它也适用于更高版本的Andr​​oid操作系统。它不知道为什么它不工作。

It's nice, definitely i'm not on totally zero but it would be nice to get it work also for higher versions of Android OS. It don't know exactly why it doesn't work.

如果我用这个简单的方法来检查实际情况:

If i use this simple method for checking actual status:

public static final boolean hasStaticIp(Context c) {
    try {
        return Settings.System.getInt(c.getContentResolver(), 
                                        Settings.System.WIFI_USE_STATIC_IP) == 1;
    }
    catch (SettingNotFoundException e) {
        Log.i(TAG, "Settings not found (" + e.geMessage() + ")");
        return false;
    }
}

这为安卓2.x和Android的同时4.x版返回true,但在第二种情况下,变化肯定不会反映在无线我发现<一href="http://stackoverflow.com/questions/10278461/how-to-configue-static-ip-netmask-gateway-programmatically-on-android-3-x-or-4?lq=1">this有点硬codeD的解决方案如预期,但没有奏效。

it returns true for both Android 2.x and also Android 4.x but in second case, changes are definitely not reflected in Wifi I found this a little hardcoded solution but it didn't work as expected.

是否有针对面临同样的问题,任何人吗?

Is there anyone that faced against same problem?

我会很高兴的任何可行的解决方案,也为扎根设备(在linux也许有些命令),因为它很容易检查手机是否是植根与否状态。

I'll be glad for any working solution and also for rooted devices (maybe some command in linux) since it's easy to check status of whether cellphone is rooted or not.

在此先感谢。

推荐答案

现在我可以说:我抓住它

我花了许多天寻找更好,更清洁,为100%的工作怎么解决实际(现在我目前想只有一个解决方案)与反思的解决方案。但没有结果。

I spent many days by looking for better, cleaner and for 100% working solution how actual (now i guess currently only one solution) solution with reflection is. But without result.

所以,我再次试图利用与反思在这里提到的解决方案:

So i tried again to use mentioned solution with reflection here:

  • <一个href="http://stackoverflow.com/questions/10278461/how-to-configue-static-ip-netmask-gateway-programmatically-on-android-3-x-or-4?lq=1">How以configue静态IP,子网掩码,网关编程方式 安卓3.x或4.x的

和令人惊讶它的作品!现在我知道我失踪了。所以,大家谁试图在设备上该解决方案具有 API 大于 10 (SICE蜂窝)确保你所谓的:

and suprisingly it works! And now i know what i was missing. So everyone who tried this solution on device with API greater than 10 (sice Honeycomb) make sure you called:

wifiManager.saveConfiguration();

这还不够只是叫

it's not enough to call only

wifiManager.updateNetwork(wifiConfiguration);

bacause变化(通过反射也做了)不会被永久保存到特定的 WifiConfiguration

所以,现在它按预期工作,现在一个小小的总结:

So now it works as expected and now a little summary:

安卓1.x和2.x不适合(仅适用于实际连接的网络)那么简单可行的解决方案设置每个SSID静态IP地址提供的解决方案是使用 ContentResolver的和数据写入< STRONG>系统设置通过:

Android 1.x and 2.x doesn't provide solution for setting static ip address per SSID (only for actual connected network) so simple working solution is to use ContentResolver and write data to System Settings via:

Settings.System.putInt(resolver, "wifi_use_static_ip", 1); // enabling static ip
Settings.System.putInt(resolver, "wifi_use_static_ip", 0); // enabling DHCP

设置静态IP地址为Android 3.x和4.x:

由于Android 3.X它可能每个SSID设置静态IP地址更低版本的Andr​​oid操作系统,因此解决方案将无法正常工作。

Setting static IP address for Android 3.x and 4.x:

Since Android 3.x it's possible to set static ip address per SSID hence solution for lower versions of Android OS will not working.

目前没有 API 为了这个目的,以便与反思上面链接的解决方案只有一个,就是实际工作。

Currently there is no API for this purpose so solution with reflection linked above is only one that actually works.

不要忘了改 setGateway()为Android 3.X(在原产地的线程也提到)

Don't forget to change setGateway() for Android 3.x (also mentioned in origin thread)

最后,因为如果有人想为设置静态IP联系地址为Android 3.x和4.x适当的方法应用程序需要的网络掩码preFIX为INT 未满子网掩码 可用网络掩码列表与他们的prefixes。

Finally since if someone want to have application for setting static ip adress for Android 3.x and 4.x proper method takes netmask prefix as int and not full netmask so here is list of available netmasks with their prefixes.

希望它帮助。

这篇关于分配静态IP地址在Android 3.x和4.x Wifi网络的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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