WifiManager 无法使用 Android Studio 在 API 29 及更高版本上工作
WifiManager not working on API 29 and higher using Android Studio
所以我尝试将 wifi 管理器的代码用于 disable/enable wifi,我的应用程序的目标设备仅适用于 API 29 及更高版本。我刚刚发现 WifiManager 只适用于旧版本。有没有其他方法可以在更高版本上运行?
仅供参考:
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE);
wifiManager.setWifiEnabled(true);
由于安全原因 setWifiEnabled
在 Android 10+ 中被弃用
This method was deprecated in API level 29.
Starting with Build.VERSION_CODES#Q, applications are not allowed to enable/disable Wi-Fi. Compatibility Note: For applications targeting Build.VERSION_CODES.Q or above, this API will always fail and return false. If apps are targeting an older SDK (Build.VERSION_CODES.P or below), they can continue to use this API.
现在您唯一的方法是将用户传递到设置 GUI,其中 Wi-Fi 只能手动启用
Intent settingsIntent = new Intent(Settings.Panel.ACTION_INTERNET_CONNECTIVITY);
startActivityForResult(settingsIntent);
所以我尝试将 wifi 管理器的代码用于 disable/enable wifi,我的应用程序的目标设备仅适用于 API 29 及更高版本。我刚刚发现 WifiManager 只适用于旧版本。有没有其他方法可以在更高版本上运行?
仅供参考:
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE);
wifiManager.setWifiEnabled(true);
setWifiEnabled
在 Android 10+ 中被弃用
This method was deprecated in API level 29. Starting with Build.VERSION_CODES#Q, applications are not allowed to enable/disable Wi-Fi. Compatibility Note: For applications targeting Build.VERSION_CODES.Q or above, this API will always fail and return false. If apps are targeting an older SDK (Build.VERSION_CODES.P or below), they can continue to use this API.
现在您唯一的方法是将用户传递到设置 GUI,其中 Wi-Fi 只能手动启用
Intent settingsIntent = new Intent(Settings.Panel.ACTION_INTERNET_CONNECTIVITY);
startActivityForResult(settingsIntent);