当前位置:   article > 正文

android 教你如何判断是否是模拟器还是真机_真手机与模拟器检测

真手机与模拟器检测

最近有一些业务需求要判断是否在真机上运行还是在模拟器上运行两种不同的情况下做不同的业务逻辑操作。上网查了查还真有不少的资源。接下来老于接给大家们来总结一下。主要思路:

1.  首先要明白 不要采用IMEI的方式。模拟器的IMEI可以修改的。而且平板是没有IMEI的,可以检测设备的MAC地址,模拟器的MAC地址是固定的几种。

2.通过调用公开或者隐藏的系统API判断并不靠谱,因为调用结果可以轻易被修改,比如直接修改Android的源代码或者借助Xposed Framework进行修改。
3.有基于模拟器特征和api返回值的检测方法都可以通过修改安卓源码的方式轻松绕过。模拟器与真机的本质区别在于运行载体。鉴于大多数的安卓模拟器基于qemu,qemu在执行程序时实际上是将其翻译成宿主机的指令,比如将安卓的arm指令翻译成PC的x86指令。为了效率上的考虑,qemu在翻译执行arm指令时并没有实时更新模拟的pc寄存器值,只会在一段代码翻译执行完之后再更新,而真机中pc寄存器是一直在更新的。基于这一点,可以设计一段CPU任务调度程序来检测模拟器 。具体的你可以参鉴DexLab上的一篇文章。当然,这个方法也是可以被绕过的,可以在理解qemu源码的基础上,修改qemu源码,但很明显这个门槛很高 。

  1. // 系统在启动的时候会做一次判断,看看SystemServer.java里是怎么判断的:
  2. private void initData(){
  3. boolean isEmulator = SystemProperties.get("ro.kernel.qemu").equals("1");
  4. if (isEmulator) {
  5. Slog.i(TAG, "No Bluetooh Service (emulator)");
  6. } else if (mFactoryTestMode == FactoryTest.FACTORY_TEST_LOW_LEVEL) {
  7. Slog.i(TAG, "No Bluetooth Service (factory test)");
  8. } else if (!context.getPackageManager().hasSystemFeature
  9. (PackageManager.FEATURE_BLUETOOTH)) {
  10. Slog.i(TAG, "No Bluetooth Service (Bluetooth Hardware Not Present)");
  11. } else if (disableBluetooth) {
  12. Slog.i(TAG, "Bluetooth Service disabled by config");
  13. } else {
  14. Slog.i(TAG, "Bluetooth Service");
  15. mSystemServiceManager.startService(BluetoothService.class);
  16. }
  17. }
  系统在启动的时候会做一次判断,看看SystemServer.java里是怎么判断的:
  1. private void initData(){
  2. boolean isEmulator = SystemProperties.get("ro.kernel.qemu").equals("1");
  3. if (isEmulator) {
  4. Slog.i(TAG, "No Bluetooh Service (emulator)");
  5. } else if (mFactoryTestMode == FactoryTest.FACTORY_TEST_LOW_LEVEL) {
  6. Slog.i(TAG, "No Bluetooth Service (factory test)");
  7. } else if (!context.getPackageManager().hasSystemFeature
  8. (PackageManager.FEATURE_BLUETOOTH)) {
  9. Slog.i(TAG, "No Bluetooth Service (Bluetooth Hardware Not Present)");
  10. } else if (disableBluetooth) {
  11. Slog.i(TAG, "Bluetooth Service disabled by config");
  12. } else {
  13. Slog.i(TAG, "Bluetooth Service");
  14. mSystemServiceManager.startService(BluetoothService.class);
  15. }
  16. }
 //第二种方式   读取文件内容,然后检查已知QEmu的驱动程序的列表
private static String[]known_qemu_drivers = {"goldfish" };
public static Boolean CheckQEmuDriverFile(){
    File driver_file =new File("/proc/tty/drivers");
    if(driver_file.exists()&& driver_file.canRead()){
        byte[]data =new byte[(int)driver_file.length()];
        try{
            InputStream inStream =new FileInputStream(driver_file);
            inStream.read(data);
            inStream.close();
        }catch (FileNotFoundException e){
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        }catch (IOException e){
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        }
        String driver_data =new String(data);
        for(String known_qemu_driver : known_qemu_drivers){
            if(driver_data.indexOf(known_qemu_driver)!= -1){
                Log.v("Result:","Find known_qemu_drivers!");
                return true;
            }
        }
    }
    Log.v("Result:","Not Find known_qemu_drivers!");
    return false;
}
第三种方式检测模拟器上特有的几个文件
private static String[]known_numbers = {"15555215554","15555215556",
        "15555215558","15555215560","15555215562","15555215564",
        "15555215566","15555215568","15555215570","15555215572",
        "15555215574","15555215576","15555215578","15555215580",
        "15555215582","15555215584",};

public static Boolean CheckPhoneNumber(Context context){
    TelephonyManager telephonyManager =(TelephonyManager)context
            .getSystemService(Context.TELEPHONY_SERVICE);

    String phonenumber =telephonyManager.getLine1Number();

    for(String number :known_numbers){
        if(number.equalsIgnoreCase(phonenumber)){
            Log.v("Result:","Find PhoneNumber!");
            return true;
        }
    }
    Log.v("Result:","Not Find PhoneNumber!");
    return false;
}
//第五中方式 ----》 检测设备IDS 是不是 “000000000000000”
private static String[]known_device_ids = {"000000000000000" // 默认ID
};

public static Boolean CheckDeviceIDS(Context context){
    TelephonyManager telephonyManager = (TelephonyManager)context
            .getSystemService(Context.TELEPHONY_SERVICE);

    String device_ids =telephonyManager.getDeviceId();

    for(String know_deviceid : known_device_ids){
        if(know_deviceid.equalsIgnoreCase(device_ids)){
            Log.v("Result:","Find ids: 000000000000000!");
            return true;
        }
    }
    Log.v("Result:","Not Find ids: 000000000000000!");
    return false;
}
//第六种方式 检测imsi id是不是“310260000000000”
  1. private static String[]known_imsi_ids = {"310260000000000" // 默认的 imsi id
  2. };
  3. public static Boolean CheckImsiIDS(Context context){
  4. TelephonyManager telephonyManager =(TelephonyManager)
  5. context.getSystemService(Context.TELEPHONY_SERVICE);
  6. String imsi_ids =telephonyManager.getSubscriberId();
  7. for(String know_imsi :known_imsi_ids){
  8. if(know_imsi.equalsIgnoreCase(imsi_ids)){
  9. Log.v("Result:","Find imsi ids: 310260000000000!");
  10. return true;
  11. }
  12. }
  13. Log.v("Result:","Not Find imsi ids: 310260000000000!");
  14. return false;
  15. }
//第七种方式   检测手机上的一些硬件信息
public static Boolean CheckEmulatorBuild(Context context){
    String BOARD =android.os.Build.BOARD;
    String BOOTLOADER =android.os.Build.BOOTLOADER;
    String BRAND =android.os.Build.BRAND;
    String DEVICE =android.os.Build.DEVICE;
    String HARDWARE =android.os.Build.HARDWARE;
    String MODEL =android.os.Build.MODEL;
    String PRODUCT =android.os.Build.PRODUCT;
    if(BOARD== "unknown"|| BOOTLOADER== "unknown"
            ||BRAND =="generic" ||DEVICE =="generic"
            ||MODEL =="sdk" ||PRODUCT =="sdk"
            ||HARDWARE =="goldfish")
    {
        Log.v("Result:","Find Emulator by EmulatorBuild!");
        return true;
    }
    Log.v("Result:","Not Find Emulator by EmulatorBuild!");
    return false;
}
//第八种方式 ---》   检测手机运营商家
public static boolean CheckOperatorNameAndroid(Context context){
    String szOperatorName = ((TelephonyManager)
            context.getSystemService(Context.TELEPHONY_SERVICE)).getNetworkOperatorName();

    if(szOperatorName.toLowerCase().equals("android")== true){
        Log.v("Result:","Find Emulator by OperatorName!");
        return true;
    }
    Log.v("Result:","Not Find Emulator by OperatorName!");
    return false;
}
最后要注意的是在PC操作系统上的某些恶意软件会判断是不是在虚拟机上运行。
老于的博客地址: http://blog.csdn.net/androidstarjack
另外你觉得此篇文章对您有所帮助 请加 QQ交流群 :232203809    
此外还可以关注终端研发部


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

闽ICP备14008679号