- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
如何在 Android 5.x 上以编程方式配置静态 IP 地址、网络掩码、网关、DNS 以进行 Wi-Fi 连接?是否有一个开放的 API(没找到)或隐藏的可以用于此?如果可能的话,您能否再举一些例子。
我知道它是 possible on Android 4.0+但它不适用于 Android 5.0
最佳答案
不幸的是,仍然没有开放的 API。
适用于 Android 4.0 的解决方案在 LOLLIPOP 中不起作用,因为内容已被移动。特别是新的 IpConfiguration
类现在包含 StaticIpConfiguration
以及所有这些领域。它们仍然可以通过像这样的方式使用反射(具有所有的脆弱性)来访问。
警告,此代码仅适用于 Android 5.0。您需要检查 Build.VERSION.SDK_INT
并采取相应措施。
@SuppressWarnings("unchecked")
private static void setStaticIpConfiguration(WifiManager manager, WifiConfiguration config, InetAddress ipAddress, int prefixLength, InetAddress gateway, InetAddress[] dns) throws ClassNotFoundException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, NoSuchFieldException, InstantiationException
{
// First set up IpAssignment to STATIC.
Object ipAssignment = getEnumValue("android.net.IpConfiguration$IpAssignment", "STATIC");
callMethod(config, "setIpAssignment", new String[] { "android.net.IpConfiguration$IpAssignment" }, new Object[] { ipAssignment });
// Then set properties in StaticIpConfiguration.
Object staticIpConfig = newInstance("android.net.StaticIpConfiguration");
Object linkAddress = newInstance("android.net.LinkAddress", new Class<?>[] { InetAddress.class, int.class }, new Object[] { ipAddress, prefixLength });
setField(staticIpConfig, "ipAddress", linkAddress);
setField(staticIpConfig, "gateway", gateway);
getField(staticIpConfig, "dnsServers", ArrayList.class).clear();
for (int i = 0; i < dns.length; i++)
getField(staticIpConfig, "dnsServers", ArrayList.class).add(dns[i]);
callMethod(config, "setStaticIpConfiguration", new String[] { "android.net.StaticIpConfiguration" }, new Object[] { staticIpConfig });
manager.updateNetwork(config);
manager.saveConfiguration();
}
使用以下辅助方法来处理反射:
private static Object newInstance(String className) throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, IllegalArgumentException, InvocationTargetException
{
return newInstance(className, new Class<?>[0], new Object[0]);
}
private static Object newInstance(String className, Class<?>[] parameterClasses, Object[] parameterValues) throws NoSuchMethodException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, ClassNotFoundException
{
Class<?> clz = Class.forName(className);
Constructor<?> constructor = clz.getConstructor(parameterClasses);
return constructor.newInstance(parameterValues);
}
@SuppressWarnings({ "unchecked", "rawtypes" })
private static Object getEnumValue(String enumClassName, String enumValue) throws ClassNotFoundException
{
Class<Enum> enumClz = (Class<Enum>)Class.forName(enumClassName);
return Enum.valueOf(enumClz, enumValue);
}
private static void setField(Object object, String fieldName, Object value) throws IllegalAccessException, IllegalArgumentException, NoSuchFieldException
{
Field field = object.getClass().getDeclaredField(fieldName);
field.set(object, value);
}
private static <T> T getField(Object object, String fieldName, Class<T> type) throws IllegalAccessException, IllegalArgumentException, NoSuchFieldException
{
Field field = object.getClass().getDeclaredField(fieldName);
return type.cast(field.get(object));
}
private static void callMethod(Object object, String methodName, String[] parameterTypes, Object[] parameterValues) throws ClassNotFoundException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException
{
Class<?>[] parameterClasses = new Class<?>[parameterTypes.length];
for (int i = 0; i < parameterTypes.length; i++)
parameterClasses[i] = Class.forName(parameterTypes[i]);
Method method = object.getClass().getDeclaredMethod(methodName, parameterClasses);
method.invoke(object, parameterValues);
}
例如,你可以这样调用它:
public void test(Context context)
{
WifiManager manager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
WifiConfiguration wifiConf = ... /* Get Wifi configuration you want to update */
if (wifiConf != null)
{
try
{
setStaticIpConfiguration(manager, wifiConf,
InetAddress.getByName("10.0.0.1"), 24,
InetAddress.getByName("10.0.0.2"),
new InetAddress[] { InetAddress.getByName("10.0.0.3"), InetAddress.getByName("10.0.0.4") });
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
作为引用,您可能想看看 WifiConfigController
框架中的类(尽管它直接使用这些类而不是通过反射)。
关于java - 如何在 Android 5.x (Lollipop) 上以编程方式为 Wi-Fi 连接配置静态 IP 地址、网络掩码、网关、DNS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27599564/
我使用工具栏是为了在我的应用程序中进行 Material 设计。一切正常,但除了更改菜单项文本颜色时,我完全无法接受该解决方案。我还张贴了我在申请中使用的文本和代码的屏幕截图,供您引用。我尝试了几种替
看起来 FloatingActionButton 在 Android 4.0 和 Lollipop 上无法正常工作。正如您在下图中看到的那样,在 Android Lollipo 上,阴影缺失,而在 A
SDK 升级到 Android 5 后,我无法使用 Intel 硬件加速执行管理器: $ android-sdk-macosx/tools/emulator -avd AVD_for_LowMemor
SDK升级到Android 5后,无法使用Intel Hardware Accelerated Execution Manager: $ android-sdk-macosx/tools/emulat
我有不同 draw9patch png 作为背景的按钮。目前按钮由 selector 控制,看起来像这样: 对于 Android Lollipop,他们有一个用于触摸效果的
我尝试在 Pre-Lollipop 设备上运行我的应用程序。但是这段代码向我显示了 Pre-Lolliop 和 Lollipop 设备的相同错误。 如果我将“android:”添加到样式中(如样式 2
我实现了一个自定义的 SurfaceView 来绘制相机预览,并在其上完成了所有捕获和手动对焦操作。它在 Pre-Lolipop 设备上运行良好,但问题是它在 Lollipop 设备上崩溃。 发生的最
风格 @color/my_primary_color @color/my_primary_dark_color @color/accent 布局
我想在 Lollipop 设备中显示较旧的时间选择器(如 Lollipop 设备之前的时间选择器)。可以 这是可能的。 最佳答案 您可以通过将 timePickerMode 属性设置为“spinner
嗨,所以我有点困惑,想知道是否有人能指出我正确的方向。 在 Lollipop 和 pre-lollipop 上使用 Google Play 商店 您会在 Lollipop 上看到可选择的 View 具
如果我们希望使用 Material Design 的应用同时支持 Lollipop 和 Lollipop 之前的设备,以下哪种方法是正确的? 单独使用 Android 支持库:仅对整个应用程序使用支持
我正在使用 appcompat/support-v7 中引入的新工具栏小部件。我想根据用户是否向上/向下滚动页面来隐藏/显示工具栏,就像在新的 Google Playstore 应用程序或 NewsS
我的数据库中有一个 .mhtml 文件作为字节数组。我写了下面的代码,它在 Lollipop 之前的设备上运行良好。但它不适用于 Lollipop 和棉花糖。 代码:- String p
根据标题,我能够在下面的 Lollipop 中成功读取/写入数据,但在 Lollipop 中多次读取/写入失败。 这就是我读/写数据的方式: boolean isRead= getmBluetooth
我刚刚在 Android Studio 上安装了 Android 5,并在模拟器上进行了第一次运行。我发现“API Demos”应用程序非常有趣,我想找到代码。我说的是这个应用程序: 有人知道我在哪里
我正在关注 this tutorial在 Android Material Design 中使用颜色(我的 ActionBar 和 StatusBar)。我确实按照教程进行操作,但没有反射(refle
在我的应用程序中,用户可以通过它调用任何号码,我正在启动蓝牙 audioManager.setBluetoothScoOn(true); audioManager.startBluetoothSco(
我正在我的 android 应用程序中实现 Google map API v2。该应用程序在所有设备上都可以正常工作,但在 Lollipop 设备上却不行。应用程序在 Lollipop 中崩溃。我确实
我正在开发一个项目,其中有一个日期选择器。我正在尝试为其设置最大日期和最短日期。最短日期是今天之后 1 天,最大日期是今天之后 11 天。但问题是在 Lollipop 设备中,最后一个最大日期显示为可
我正在使用嵌入式无线系统,它以定义的时间间隔生成数据包。目的是检测设备/系统何时重新启动。为此,我读到 Lollipop 序列编号是最适合此目的的编号方案。 “在此编号方案中,序列号从负值开始,增加直
我是一名优秀的程序员,十分优秀!