简体   繁体   English

仅在物理设备而非模拟器上运行 Android 应用

[英]Run Android app Only on Physical Device not Emulator

How can I make my app run only on a physical android device, not an emulator.如何让我的应用程序仅在物理 android 设备上运行,而不是在模拟器上运行。

When the app starts I want to check if the device is a physical device or emulator.当应用程序启动时,我想检查设备是物理设备还是模拟器。 If it is an emulator, I want my app to stop.如果它是模拟器,我希望我的应用程序停止。

How can I do this?我怎样才能做到这一点?

In the onCreate() method of your launch activity, you can check whether the device is running on an emulator and, if it is, just call finish() .在启动活动的onCreate()方法中,您可以检查设备是否正在模拟器上运行,如果是,只需调用finish() To check whether you're running on an emulator, you can use something like the following code (taken from this answer ):要检查您是否在模拟器上运行,您可以使用类似以下代码的内容(取自此答案):

public static boolean isEmulator() {
    return Build.FINGERPRINT.startsWith("generic")
            || Build.FINGERPRINT.startsWith("unknown")
            || Build.MODEL.contains("google_sdk")
            || Build.MODEL.contains("Emulator")
            || Build.MODEL.contains("Android SDK built for x86")
            || Build.MANUFACTURER.contains("Genymotion")
            || (Build.BRAND.startsWith("generic") && Build.DEVICE.startsWith("generic"))
            || "google_sdk".equals(Build.PRODUCT);
}

You can find lots of other suggestions on the web for detecting an emulator environment.您可以在网络上找到许多其他建议来检测模拟器环境。 I don't know of any that are absolutely foolproof, but the above is pretty robust.我不知道有什么绝对万无一失的,但上面的内容非常强大。

You can try something like below您可以尝试以下操作

boolean isEmulator() {
        return (Build.BRAND.startsWith("generic") && Build.DEVICE.startsWith("generic"))
                || Build.FINGERPRINT.startsWith("generic")
                || Build.FINGERPRINT.startsWith("unknown")
                || Build.HARDWARE.contains("goldfish")
                || Build.HARDWARE.contains("ranchu")
                || Build.HARDWARE.equals("vbox86")
                || Build.HARDWARE.toLowerCase().contains("nox")
                || Build.MODEL.contains("google_sdk")
                || Build.MODEL.contains("Emulator")
                || Build.MODEL.contains("Android SDK built for x86")
                || Build.MODEL.toLowerCase().contains("droid4x")
                || Build.MANUFACTURER.contains("Genymotion")
                || Build.PRODUCT.contains("sdk_google")
                || Build.PRODUCT.contains("google_sdk")
                || Build.PRODUCT.contains("sdk")
                || Build.PRODUCT.contains("sdk_x86")
                || Build.PRODUCT.contains("vbox86p")
                || Build.PRODUCT.contains("emulator")
                || Build.PRODUCT.contains("simulator")
                || Build.PRODUCT.toLowerCase().contains("nox")
                || Build.BOARD.toLowerCase().contains("nox")
                || Build.BOOTLOADER.toLowerCase().contains("nox")
                || Build.SERIAL.toLowerCase().contains("nox");
    }

It's an update of the code used in the Flutter project(device info plugin).它是 Flutter 项目(设备信息插件)中使用的代码的更新。 check it here 在这里检查

You can also use code below, It worked for me and the code is very less: 您还可以在下面使用代码,它对我有用,并且代码很少:

public static final String DSN_EMULATOR = "unknown";

  if(Build.SERIAL.equalsIgnoreCase(AppConstant.DSN_EMULATOR) || Build.SERIAL.contains("EMULATOR")) {
  // It's an emulator
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法在 Android 模拟器或 Android 物理设备中运行 Flutter 项目 - Not able to run Flutter Project in Android Emulator or Android Physical Device 无法在物理设备或模拟器上运行 flutter 应用程序 - can't run flutter app on physical device or emulator 为什么Android应用无法在物理设备上运行? - why android app does not run on physical device? 如何在 Android Studio 中将我的物理设备作为模拟器运行? - How can I run my physical device as an emulator in Android studio? Xamarin Forms / Android map app在模拟器上运行,但在物理设备上崩溃 - Xamarin Forms/Android map app runs on emulator, but crashes on physical device Flutter 应用程序在 Android 模拟器上运行,但不在物理设备上(OnePlus 7T) - Flutter app is running on Android Emulator but not on Physical Device (OnePlus 7T) Android应用程序无法在设备或模拟器上运行吗? - Android app don't run on the device or emulator? Android应用只能在模拟器上运行,而不能在设备上运行 - Android app runs only on emulator but not on device Firebase Auth 给出 ViewPostIme 指针 1 在物理设备上运行但在 Android 模拟器上工作(Firebase 仅在模拟器上工作) - Firebase Auth gives ViewPostIme pointer 1 While running on a physical device but work on Android Emulator ( Firebase works only on emulator ) 将 android 仿真器连接为物理 android 设备 - connecting android emulator as a physical android device
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM