2016-05-17 8 views
5

Per iPhone posso rilevare che l'applicazione è in esecuzione in un simulatore in questo modo:Xamarin.Android Rileva emulatore

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

Qual è la migliore equivalente per rilevare l'emulatore in Xamarin.Android?

risposta

4

Essa dipende il tuo obiettivo di se questo è solo per il test di debug locale o se avete intenzione di lasciarlo nel codice per il test in un ambiente utente finale.

Mentre il mondo di Android è abbastanza grande, questo è un metodo in continua evoluzione in base a quello che abbiamo visto in natura:

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; 
} 

Aggiornato: Corretto il rilevamento emulatore XAP su più piattaforme

2

Source

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

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

FYI: un sacco di telefoni (basati su cinesi e build basati su CyanogenMod) contengono 'generic' nelle loro impronte digitali. – SushiHangover

Problemi correlati