关闭 Android Q 中的 Wi-Fi。

huangapple go评论65阅读模式
英文:

turn off wifi in Android Q

问题

除了标准的ConnectivityManager API之外,在Android Q中修改Wi-Fi状态的其他方法吗?我刚刚看到Google在Android 10中删除了该API。我愿意为应用程序授予Android设备管理员权限,使用adb授予所有权限,等等,因为我不会发布该应用程序,只会自己使用。

英文:

Is there any way besides the standard ConnectivityManager-API to modify wifi state in android q? 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.

答案1

得分: 2

Your app must be a device owner as a variant to use wifiManager.setWifiEnabled(false) method that is deprecated started from Q.

Deprecation Exemptions:
Device Owner (DO), Profile Owner (PO) and system apps.
WifiManager

英文:

Your app must be a device owner as a variant to use wifiManager.setWifiEnabled(false) method that is deprecated started from Q.

Deprecation Exemptions:
Device Owner (DO), Profile Owner (PO) and system apps.
WifiManager

答案2

得分: -1

你可以使用以下代码来禁用Wi-Fi:

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

断开Wi-Fi连接的代码如下:

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

要忘记特定的Wi-Fi网络,可以使用以下代码:

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

You can use the following code to disable 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 :

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

and the following code to forget a particular wifi network :

 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()) &amp;&amp; wifiManager.saveConfiguration();
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }

huangapple
  • 本文由 发表于 2020年1月6日 15:51:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/59608457.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定