当前位置:   article > 正文

如何检测模拟器中何时运行了Android应用程序?_模拟器中监控android运行

模拟器中监控android运行

本文翻译自:How can I detect when an Android application is running in the emulator?

I would like to have my code run slightly differently when running on the emulator than when running on a device. 我希望在模拟器上运行的代码与在设备上运行的代码略有不同。 ( For example , using 10.0.2.2 instead of a public URL to run against a development server automatically.) What is the best way to detect when an Android application is running in the emulator? 例如 ,使用10.0.2.2代替公共URL自动针对开发服务器运行。)检测Android应用程序何时在模拟器中运行的最佳方法是什么?


#1楼

参考:https://stackoom.com/question/BkAj/如何检测模拟器中何时运行了Android应用程序


#2楼

I never found a good way to tell if you're in the emulator. 我从来没有找到一种好的方法来判断您是否在模拟器中。

but if you just need to detecet if you're in a development environment you can do this : 但是如果您只是在开发环境中需要检测,则可以执行以下操作:

  1. if(Debug.isDebuggerConnected() ) {
  2. // Things to do in debug environment...
  3. }

Hope this help.... 希望能有所帮助。


#3楼

Build.BRAND.startsWith("generic") && Build.DEVICE.startsWith("generic")

This should return true if the app is running on an emulator. 如果应用程序在模拟器上运行,则应返回true。

What we should be careful about is not detecting all the emulators because there are only several different emulators. 我们应该注意的是不会检测所有仿真器,因为只有几个不同的仿真器。 It is easy to check. 很容易检查。 We have to make sure that actual devices are not detected as an emulator. 我们必须确保没有将实际设备检测为仿真器。

I used the app called " Android Device Info Share " to check this. 我使用名为“ Android设备信息共享 ”的应用程序对此进行了检查。

On this app, you can see various kinds of information of many devices (probably most devices in the world; if the device you are using is missing from the list, it will be added automatically). 在此应用程序上,您可以看到许多设备的各种信息(可能是世界上大多数设备;如果列表中缺少您正在使用的设备,则会自动添加该信息)。


#4楼

Put a file in the file system of the emulator; 将文件放入仿真器的文件系统中; since the file won't exist on the real device, this should be stable, reliable and easy to fix when it breaks. 由于该文件在实际设备上将不存在,因此该文件应该稳定,可靠并且在损坏时易于修复。


#5楼

I tried several techniques, but settled on a slightly revised version of checking the Build.PRODUCT as below. 我尝试了几种技术,但选择了对Build.PRODUCT进行稍微修改的版本,如下所示。 This seems to vary quite a bit from emulator to emulator, that's why I have the 3 checks I currently have. 各个仿真器之间的差异似乎相差很大,这就是为什么我要拥有当前拥有的3张支票的原因。 I guess I could have just checked if product.contains("sdk") but thought the check below was a bit safer. 我想我可以检查一下product.contains(“ sdk”),但认为以下检查更安全。

  1. public static boolean isAndroidEmulator() {
  2. String model = Build.MODEL;
  3. Log.d(TAG, "model=" + model);
  4. String product = Build.PRODUCT;
  5. Log.d(TAG, "product=" + product);
  6. boolean isEmulator = false;
  7. if (product != null) {
  8. isEmulator = product.equals("sdk") || product.contains("_sdk") || product.contains("sdk_");
  9. }
  10. Log.d(TAG, "isEmulator=" + isEmulator);
  11. return isEmulator;
  12. }

FYI - I found that my Kindle Fire had Build.BRAND = "generic", and some of the emulators didn't have "Android" for the network operator. 仅供参考-我发现我的Kindle Fire拥有Build.BRAND =“ generic”,并且某些仿真器没有网络运营商的“ Android”。


#6楼

根据其他答案的提示,这可能是最可靠的方法:

isEmulator = "goldfish".equals(Build.HARDWARE)

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/261845
推荐阅读
相关标签
  

闽ICP备14008679号