gpt4 book ai didi

android - 如何在 Android 8.0 (Oreo) 中以编程方式打开/关闭 wifi 热点

转载 作者:IT老高 更新时间:2023-10-28 23:30:03 24 4
gpt4 key购买 nike

我知道如何使用以下方法在 android 中使用反射打开/关闭 wifi 热点。

private static boolean changeWifiHotspotState(Context context,boolean enable) {
try {
WifiManager manager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
Method method = manager.getClass().getDeclaredMethod("setWifiApEnabled", WifiConfiguration.class,
Boolean.TYPE);
method.setAccessible(true);
WifiConfiguration configuration = enable ? getWifiApConfiguration(manager) : null;
boolean isSuccess = (Boolean) method.invoke(manager, configuration, enable);
return isSuccess;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}

但是上面的方法在Android 8.0(Oreo)上不行。

当我在 Android 8.0 中执行上述方法时,我在 logcat 中得到以下语句。

com.gck.dummy W/WifiManager: com.gck.dummy attempted call to setWifiApEnabled: enabled = true

在android 8.0上有没有其他方法可以打开/关闭热点

最佳答案

我认为 LocalOnlyHotspot 路线是通往的方式,但正如@edsappfactory.com 在评论中所说 - 它只提供封闭的网络,没有互联网访问。

在 Oreo 中,热点/网络共享移至 ConnectionManager,其注释为 @SystemApi,因此(名义上)无法访问。

作为我正在做的其他事情的一部分,我制作了一个应用程序并将其放在 github here .它使用反射来获取函数和DexMaker生成 ConnectionManager.OnStartTetheringCallback 的子类(这也是不可访问的)。

认为这一切都很好 - 边缘有点粗糙,所以请随时改进!

相关代码在:

我失去了耐心,试图让我的 DexMaker 生成的回调触发 MyOnStartTetheringCallback所以所有的代码都乱七八糟并被注释掉了。

关于android - 如何在 Android 8.0 (Oreo) 中以编程方式打开/关闭 wifi 热点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45984345/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com