Xamarin.Android检测模拟器 [英] Xamarin.Android Detect Emulator

查看:122
本文介绍了Xamarin.Android检测模拟器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

For iPhone I can detect that the app is running in a Simulator by doing this:

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

在Xamarin.Android中检测模拟器的最佳等效方法是什么?

What is the best equivalent for detecting the emulator in Xamarin.Android?

推荐答案

这取决于您的目标是仅用于本地调试测试,还是打算将其保留在代码中以在最终用户环境中进行测试.

It depends upon your goal of if this is just for local debug testing or if you plan to leave it in your code for testing within an end-user environment.

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

As the world of Android is quite large, this is an ever evolving method based upon what we seen in the wild:

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仿真器检测


Updated: Fixed XAP emulator detection on multiple platforms

这篇关于Xamarin.Android检测模拟器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆