在 android 上以编程方式启用/禁用 USB 或 Wifi 网络共享
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3531801/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Enable/Disable USB or Wifi tethering programmatically on android
提问by Gaurav Khurana
Is there a way to enable or disable tethering (USB or wifi) on an android phone programmatically? Perhaps an API in android SDK or NDK or any even non-UI command to do that.
有没有办法以编程方式在 Android 手机上启用或禁用网络共享(USB 或 wifi)?也许 android SDK 或 NDK 中的 API 或任何甚至非 UI 命令来执行此操作。
Thanks in advance.
提前致谢。
回答by iTech
It is possibleand withoutroot access, I use the below code snipped in my apps:
这是可能的,并且没有root 访问权限,我使用在我的应用程序中剪下的以下代码:
private void setWifiTetheringEnabled(boolean enable) {
WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
Method[] methods = wifiManager.getClass().getDeclaredMethods();
for (Method method : methods) {
if (method.getName().equals("setWifiApEnabled")) {
try {
method.invoke(wifiManager, null, enable);
} catch (Exception ex) {
}
break;
}
}
}
Your app should have the following permission:
您的应用应具有以下权限:
android.permission.CHANGE_WIFI_STATE
android.permission.CHANGE_WIFI_STATE
回答by matto1990
Take a look at this question.
看看这个问题。
Basically there are no public API's to do it. Take a look at the settings app to see how internal apps do it though. Might have some luck with that:
基本上没有公共 API 可以做到这一点。看看设置应用程序,看看内部应用程序是如何做到的。可能有一些运气: