英文:
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()) && wifiManager.saveConfiguration();
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论