简体   繁体   English

在 Android Q 中关闭 wifi

[英]turn off wifi in Android Q

Is there any way besides the standard ConnectivityManager -API to modify wifi state in android q?除了标准的ConnectivityManager -API 之外,还有什么方法可以修改 android q 中的 wifi 状态? I've just read google removed the api in android 10. Im willing to give the app android device administrator status, grant all permissions with adb, etc as I wont publish the app and will only use it for myself.我刚刚读过谷歌在 android 10 中删除了 api。我愿意为应用程序提供 android 设备管理员状态,使用 adb 授予所有权限等,因为我不会发布应用程序,只会为自己使用它。

Your app must be a device owner as a variant to use wifiManager.setWifiEnabled(false) method that is deprecated started from Q.您的应用必须是设备所有者作为变体才能使用从 Q 开始已弃用的wifiManager.setWifiEnabled(false)方法。

Deprecation Exemptions: Device Owner (DO), Profile Owner (PO) and system apps.弃用豁免:设备所有者 (DO)、配置文件所有者 (PO) 和系统应用程序。 WifiManager 无线管理器

You can use the following code to disable wifi :您可以使用以下代码禁用 wifi :

 try {
            WifiManager wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
            return wifiManager.setWifiEnabled(false);
        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        }

to disconnect wifi :断开wifi:

 WifiManager wifiManager = (WifiManager)context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
 wifiManager.disconnect();

and the following code to forget a particular wifi network :以及以下代码以忘记特定的 wifi 网络:

 try {
            WifiManager wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
            List wifiNetworks = wifiManager.getConfiguredNetworks();
            for (Object wifiConf : wifiNetworks) {
                if (((WifiConfiguration) wifiConf).SSID.equalsIgnoreCase(SSID)) {
                    return wifiManager.removeNetwork(wifiManager.getConnectionInfo().getNetworkId()) && wifiManager.saveConfiguration();
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM