如何判断Android设备是否为模拟器

 

android.os.Build.BRAND:获取设备品牌

 

如果获取的Landroid/os/Build;->BRAND的值 "generic"则为模拟器上运行。

android.os.Build.MODEL :获取手机的型号 设备名称。

如果发现Landroid/os/Build;->MODEL 为"sdk",则为模拟器上运行。

代码示例:

 1     const-string v0, "phone"
 2 
 3     invoke-virtual {p0, v0}, Lcom/xxx/LoadingActivity;->getSystemService(Ljava/lang/String;)Ljava/lang/Object;
 4 
 5     move-result-object v0
 6 
 7     check-cast v0, Landroid/telephony/TelephonyManager;
 8 
 9     invoke-virtual {v0}, Landroid/telephony/TelephonyManager;->getSubscriberId()Ljava/lang/String;
10 
11     sget-object v0, Landroid/os/Build;->BRAND:Ljava/lang/String;
12 
13     invoke-virtual {v0}, Ljava/lang/String;->toLowerCase()Ljava/lang/String;
14 
15     move-result-object v0
16 
17     const-string v1, "generic"
18 
19     invoke-virtual {v0, v1}, Ljava/lang/String;->indexOf(Ljava/lang/String;)I
20 
21     move-result v0
22 
23     const/16  v0, -1
24     
25     if-eq v0, v2, :cond_0
26 
27     sget-object v0, Landroid/os/Build;->MODEL:Ljava/lang/String;
28 
29     invoke-virtual {v0}, Ljava/lang/String;->toLowerCase()Ljava/lang/String;
30 
31     move-result-object v0
32 
33     const-string v1, "sdk"
34 
35     invoke-virtual {v0, v1}, Ljava/lang/String;->indexOf(Ljava/lang/String;)I
36 
37     move-result v0
38 
39     if-eq v0, v2, :cond_0
40 
41     invoke-virtual {p0}, Lcom/xxx/LoadingActivity;->finish()V
42 
43     :cond_0
44     iget-object v0, p0, Lcom/xxx/LoadingActivity;->b:Landroid/content/SharedPreferences;
45 
46     invoke-interface {v0}, Landroid/content/SharedPreferences;->edit()Landroid/content/SharedPreferences$Editor;

 

以下信息可以被用来判断是否在模拟器上运行

模拟器输出的信息
 1 C:\Users\dell>adb shell getprop
 2 
 3 [ro.kernel.qemu]:[1]
 4 [ro.hardware]:[goldfish]
 5 [ro.build.tags]:[test-keys]
 6 [ro.product.model]:[sdk]
 7 [ro.product.brand]:[generic]
 8 [ro.product.name]:[sdk]
 9 [ro.product.device]:[generic]
10 [ro.product.board]:[]
11 [ro.product.manufacturer]:[unknown]
12 [ro.board.platform]:[]
13 [ro.build.product]:[generic]
14 [ro.build.fingerprint]:[generic/sdk/generic:2.3.1/GSI11/93351:eng/test-keys]

 

你可能感兴趣的:(android)