xamarin - 运行时的移动设备或模拟器
问题描述
我只想在真正的 android\iOS 设备上运行时启用记录器服务。是否有可能在核心项目级别的运行时知道我是使用 android\iOS 模拟器还是真实设备运行?
解决方案
在 Core 项目中创建一个接口:
public interface IDevicePlatform
{
bool IsSimulator();
}
iOS DependencyService注册/实现:
public class Platform_iOS : IDevicePlatform
{
public IsSimulator()
{
return ObjCRuntime.Runtime.Arch == ObjCRuntime.Arch.SIMULATOR;
}
}
Android DependencyService注册/实现:
public class Platform_Android : IDevicePlatform
{
public bool IsSimulator()
{
if (Build.Fingerprint != null)
{
if (Build.Fingerprint.Contains("vbox") || Build.Fingerprint.Contains("generic") || Build.Fingerprint.Contains("vsemu"))
return true;
}
return false;
}
}
在 Core 中将其称为:
bool isSimulator = DependencyService.Get<IDevicePlatform>().IsSimulator();
if(isSimulator)
{
//You are running on the Simulator
}
else
{
//You are running on the real device
}
注意:iOS 实现是直截了当的,而 Android 是一个广阔的世界,所以,除了上面简单的 android 实现之外,还可以看看SushiHangover 的答案
推荐阅读
- ffmpeg - 有什么方法可以将 PNG 组合为 FFMPEG 中与 Quicktime 兼容的 MP4?
- c# - 更改我的类以使 ID 自动递增:实体框架
- react-native - 未找到隧道 URL,在运行“expo start --tunnel”命令时回退到 LAN URL
- android - android Storage Access Framework在SD卡上写入文件问题
- redux - redux:在大状态树的叶节点中设置状态
- php - Bootstrap-Select:从 Mysql 列中获取逗号值,并在 bootstrap-select 中自动显示为选中状态
- haskell - 理解 Haskell (<-) 语法糖
- javascript - 在 JavaScript 中,字符串上的 join vs += 如何以不同的方式处理 utf 编码?
- flutter - 如何在颤动中从未来构建下拉列表
- c++ - 有时地图大小小于数组