Xamarin.Android Detect Emulator

时间:2016-05-17 17:53:41

标签: android xamarin xamarin.android

对于iPhone,我可以通过执行以下操作检测到应用程序在模拟器中运行:

var isSumlator = ObjCRuntime.Runtime.Arch == ObjCRuntime.Arch.SIMULATOR;

在Xamarin.Android中检测模拟器的最佳等价物是什么?

3 个答案:

答案 0 :(得分:4)

这取决于您的目标,如果这仅用于本地调试测试,或者您是否计划将其保留在代码中以便在最终用户环境中进行测试。

由于Android的世界非常庞大,这是一种不断发展的方法,基于我们在野外看到的:

public bool isEmulator(bool LicensedPlayers = false)
{
    var detect = 0;
    try
    {
        var teleManager = (TelephonyManager)GetSystemService(TelephonyService);
        string networkOperator = "";
        try
        {
            networkOperator = teleManager.NetworkOperator;
            if (LicensedPlayers)
            {
                if ((teleManager.NetworkOperatorName == "T-Mobile") && 
                    (Build.Radio == "unknown") && 
                    (Build.Serial == "unknown") && 
                    (Build.Manufacturer == "samsung"))
                {
                    D.WriteLine("BlueStacks (OS-X) Player");
                    detect += 1;
                }
            }
        }
        catch
        {
            networkOperator = "";
            D.WriteLine("TelephonyService Exceptiion, custom emulator");
            detect += 1;
        }
        if (networkOperator.Contains("Android"))
        {
            D.WriteLine("Google's Android Emulator");
            detect += 1;
        }
    }
    catch
    {
        D.WriteLine("TelephonyService not available, custom emulator");
        detect += 1;
    }
    if (LicensedPlayers)
    {
        if (Build.Display.Contains("andy") || (Build.Hardware.Contains("andy")))
        {
            D.WriteLine("Andy Player");
            detect += 1;
        }
    }
    if (Build.Hardware.Contains("goldfish"))
    {
        D.WriteLine("Goldfish-based Emulator");
        detect += 1;
    }
    if (Build.Display.ToLowerInvariant().Contains("xamarin"))
    {
        D.WriteLine("Xamarin Android Player");
        detect += 1;
    }
    if (Build.Hardware.Contains("vsemu"))
    {
        D.WriteLine("Visual Studio Android Emulator");
        detect += 1;
    }
    if (Build.Host.Contains("genymobile") || (Build.Manufacturer.ToLowerInvariant().Contains("genymotion")))
    {
        D.WriteLine("Genymotion Android Emulator");
        detect += 1;
    }
    if (Build.Hardware.Contains("vbox") && Build.Hardware.Contains("86"))
    {
        D.WriteLine("VirtualBox-based Emulator");
        detect += 1;
    }
    return detect > 0;
}

更新:修复了多个平台上的XAP仿真器检测

答案 1 :(得分:3)

Source

string fing = Build.Fingerprint;
bool isEmulator=false;

if (fing != null) {
  isEmulator =  fing.Contains("vbox") || fing.Contains("generic") || fing.Contains("vsemu");
}

答案 2 :(得分:3)

使用Xamarin.Essentials;

var isSimulator = DeviceInfo.DeviceType == DeviceType.Virtual;