【问题标题】:Can apps recognize that they run in an android emulator?应用程序可以识别它们在 android 模拟器中运行吗?
【发布时间】:2020-03-15 14:49:24
【问题描述】:

我想知道一个 android 应用程序如何发现它正在一个 android 模拟器中运行。除此之外,如果设备刚刚启动,则没有安装用户文件和其他应用程序。有没有关于该主题的资源?

提前致谢

【问题讨论】:

标签: android android-emulator apk


【解决方案1】:

您可以了解运行您的应用的设备的详细信息。从这些细节中您可以确定该设备是模拟器还是物理设备。 请通过以下链接查看指纹、制造商、设备、型号、产品。

https://developer.android.com/reference/android/os/Build.html

例如: 在您的启动画面中,如果您输入以下代码,那么在您的 Logcat 中,您应该会看到如下所示的日志

`Log.e(TAG, "------------");
 Log.e(TAG, "Device Values");
 Log.e(TAG, "Fingerprint: " + Build.FINGERPRINT);
 Log.e(TAG, "Brand: " + Build.BRAND);
 Log.e(TAG, "Device: " + Build.DEVICE);
 Log.e(TAG, "Manufacturer: " + Build.MANUFACTURER);
 Log.e(TAG, "Model: " + Build.MODEL);
 Log.e(TAG, "Product: " + Build.PRODUCT);

 Real Device

 2020-03-15 20:46:07.136 32602-32602/com.utkarshnew.android E/NewSplashScreen:    ------------
 2020-03-15 20:46:07.136 32602-32602/com.utkarshnew.android E/NewSplashScreen:     Device Values
 2020-03-15 20:46:07.137 32602-32602/com.utkarshnew.android E/NewSplashScreen: Fingerprint: iBall/iBall_Slide_Cleo_S9/iBall_Slide_Cleo_S9:8.1.0/OPM2.1710/47218:user/release-keys
 2020-03-15 20:46:07.137 32602-32602/com.utkarshnew.android E/NewSplashScreen: Brand: iBall
 2020-03-15 20:46:07.137 32602-32602/com.utkarshnew.android E/NewSplashScreen: Device: iBall_Slide_Cleo_S9
 2020-03-15 20:46:07.137 32602-32602/com.utkarshnew.android E/NewSplashScreen: Manufacturer: iBall Slide
 2020-03-15 20:46:07.137 32602-32602/com.utkarshnew.android E/NewSplashScreen: Model: iBall Slide Cleo S9
 2020-03-15 20:46:07.137 32602-32602/com.utkarshnew.android E/NewSplashScreen: Product: iBall_Slide_Cleo_S9

 Emulator

 2020-03-15 20:53:44.725 6736-6736/com.utkarshnew.android E/NewSplashScreen: ------------
 2020-03-15 20:53:44.726 6736-6736/com.utkarshnew.android E/NewSplashScreen: Device Values
 2020-03-15 20:53:44.726 6736-6736/com.utkarshnew.android E/NewSplashScreen: Fingerprint: google/sdk_gphone_x86/generic_x86:10/QSR1.190920.001/5891938:user/release-keys
 2020-03-15 20:53:44.726 6736-6736/com.utkarshnew.android E/NewSplashScreen: Brand: google
 2020-03-15 20:53:44.726 6736-6736/com.utkarshnew.android E/NewSplashScreen: Device: generic_x86
 2020-03-15 20:53:44.726 6736-6736/com.utkarshnew.android E/NewSplashScreen: Manufacturer: Google
 2020-03-15 20:53:44.726 6736-6736/com.utkarshnew.android E/NewSplashScreen: Model: Android SDK built for x86
 2020-03-15 20:53:44.726 6736-6736/com.utkarshnew.android E/NewSplashScreen: Product: sdk_gphone_x86`

如果您看到上面的日志,您会看到对于 Emulator,Device 值将显示为 Generic,而对于物理设备,它将显示设备型号的名称。

另外,请查看这些链接

How can I detect when an Android application is running in the emulator?

How to check Android app is running in real device or virtual device?

【讨论】:

  • Log.e 用于错误记录,您可能应该使用Log.vLog.i
  • @a_local_nobody 正确。但您也可以使用它的颜色代码(红色),让特定的原木在众多原木中脱颖而出,所以不能错过。
  • 考虑到您已经可以使用 logcat 进行过滤,因此使用它是一个糟糕的主意。此外,如果您在应用程序中使用自定义错误日志记录并在生产应用程序中忘记其中之一,那么您将无缘无故地在生产应用程序上记录错误:)
猜你喜欢
  • 2014-01-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多