Xamarin 依赖服务不适用于更高的 Android 版本
Xamarin Dependency Services does not work with higher Android version
我编写了一个 Xamarin 应用程序,它通过依赖服务使用本机 Android 函数。具体来说,它是关于 WifiManager 和用于关闭 Wifi 的方法 SetWifiEnabled() 的。使用 Android 7.1 模拟器和 API 25 一切正常,我可以关闭 wifi。使用 Android 版本 11 和 API 30 的模拟器,wifi 没有关闭,我认为方法 SetWifiEnabled() 不起作用。 wifi 没有关闭。有谁知道这是为什么?
非常感谢
这是来自依赖服务的代码:
[assembly: Dependency(typeof(GetWifimanager))]
namespace WifiSwitch.Droid
{
public class GetWifimanager : IWifiManager
{
[Obsolete]
public bool CheckWifi()
{
var wifiManager = (WifiManager)Android.App.Application.Context.GetSystemService(Context.WifiService);
return wifiManager != null && wifiManager.SetWifiEnabled(false);
}
}
}
根据docs
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.
我编写了一个 Xamarin 应用程序,它通过依赖服务使用本机 Android 函数。具体来说,它是关于 WifiManager 和用于关闭 Wifi 的方法 SetWifiEnabled() 的。使用 Android 7.1 模拟器和 API 25 一切正常,我可以关闭 wifi。使用 Android 版本 11 和 API 30 的模拟器,wifi 没有关闭,我认为方法 SetWifiEnabled() 不起作用。 wifi 没有关闭。有谁知道这是为什么? 非常感谢
这是来自依赖服务的代码:
[assembly: Dependency(typeof(GetWifimanager))]
namespace WifiSwitch.Droid
{
public class GetWifimanager : IWifiManager
{
[Obsolete]
public bool CheckWifi()
{
var wifiManager = (WifiManager)Android.App.Application.Context.GetSystemService(Context.WifiService);
return wifiManager != null && wifiManager.SetWifiEnabled(false);
}
}
}
根据docs
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.