在Android上以编程方式启用/禁用USB或Wifi网络共享 [英] Enable/Disable USB or Wifi tethering programmatically on android

查看:460
本文介绍了在Android上以编程方式启用/禁用USB或Wifi网络共享的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以通过编程方式在Android手机上启用或禁用网络共享(USB或wifi)?也许是Android SDK或NDK中的API或任何甚至非UI命令都可以做到这一点.

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.

谢谢.

推荐答案

是可能的,并且没有根访问权限,我在应用程序中使用了以下代码:

It is possible and without root access, I use the below code snipped in my apps:

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

这篇关于在Android上以编程方式启用/禁用USB或Wifi网络共享的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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