看到這個標題,可能不少人會疑惑,爲啥?判斷安卓模擬器自己就不必定準確,更況且還要知道它是什麼品牌?html
是藍疊、騰訊手遊助手、網易Mumu仍是夜神模擬器呢?java
先談如何識別模擬器,百度、谷歌搜索,能找到很多參考資料。這裏我搜索到一篇相關比較全的介紹文章:Android | 檢測 Android 虛擬機的方法和代碼實現android
github 上相關的倉庫也有不少:git
https://github.com/framgia/android-emulator-detectorgithub
https://github.com/gingo/android-emulator-detectorwindows
以上這些,準確率在90%以上吧,要求不高,基本也是夠用的。那接下來的問題就是,如何判斷是什麼模擬器呢?app
…socket
…ide
…測試
若是以正常思惟來想的話,此路是不通的。我最初是有一個想法,就是任何公司/團隊提供一個模擬器讓用戶使用和下載,那它如何盈利,如何品牌露出呢?基於這點,我試了一個網易的 Mumu
我就想,那其它模擬器是否是也這樣呢?
彷佛無解了。。。
同事貼出一段代碼,從 bugly 裏面扒出來的,思路很奇特。就是判斷,是否存在某個包名的 app,若是存在就是該品牌的模擬器。
而後我試了一下,模擬器提供的應用商店是不讓卸載的,不讓卸載的,不讓卸載的(盈利主要靠從應用商店裏下載遊戲)。也就意味着存在某個應用商店,大機率就是該模擬器了(絕大部分正常狀況下)。極端狀況下就是,我用A模擬器,下載並安裝了B模擬器的應用商店,存在這種狀況,但機率很低,那咱們就遍歷全部包名,最後人肉去判斷也行。
其它幾款模擬器,均測試驗證經過,詳情可見代碼。
package com.utils; import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; import android.content.pm.ResolveInfo; import android.os.Build; import android.text.TextUtils; import java.io.File; import java.util.ArrayList; import java.util.List; public class CheckSimulator { private static final String[] PKG_NAMES = {"com.mumu.launcher", "com.ami.duosupdater.ui", "com.ami.launchmetro", "com.ami.syncduosservices", "com.bluestacks.home", "com.bluestacks.windowsfilemanager", "com.bluestacks.settings", "com.bluestacks.bluestackslocationprovider", "com.bluestacks.appsettings", "com.bluestacks.bstfolder", "com.bluestacks.BstCommandProcessor", "com.bluestacks.s2p", "com.bluestacks.setup", "com.bluestacks.appmart", "com.kaopu001.tiantianserver", "com.kpzs.helpercenter", "com.kaopu001.tiantianime", "com.android.development_settings", "com.android.development", "com.android.customlocale2", "com.genymotion.superuser", "com.genymotion.clipboardproxy", "com.uc.xxzs.keyboard", "com.uc.xxzs", "com.blue.huang17.agent", "com.blue.huang17.launcher", "com.blue.huang17.ime", "com.microvirt.guide", "com.microvirt.market", "com.microvirt.memuime", "cn.itools.vm.launcher", "cn.itools.vm.proxy", "cn.itools.vm.softkeyboard", "cn.itools.avdmarket", "com.syd.IME", "com.bignox.app.store.hd", "com.bignox.launcher", "com.bignox.app.phone", "com.bignox.app.noxservice", "com.android.noxpush", "com.haimawan.push", "me.haima.helpcenter", "com.windroy.launcher", "com.windroy.superuser", "com.windroy.launcher", "com.windroy.ime", "com.android.flysilkworm", "com.android.emu.inputservice", "com.tiantian.ime", "com.microvirt.launcher", "me.le8.androidassist", "com.vphone.helper", "com.vphone.launcher", "com.duoyi.giftcenter.giftcenter"};
private static final String[] PATHS = {"/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq", "/system/lib/libc_malloc_debug_qemu.so", "/sys/qemu_trace", "/system/bin/qemu-props", "/dev/socket/qemud", "/dev/qemu_pipe", "/dev/socket/baseband_genyd", "/dev/socket/genyd"};
private static final String[] FILES = {"/data/data/com.android.flysilkworm", "/data/data/com.bluestacks.filemanager"}; public static boolean isSimulator(Context paramContext) { try { List pathList = new ArrayList(); pathList = getInstalledSimulatorPackages(paramContext); if (pathList.size() == 0) { for (int i = 0; i < PATHS.length; i++) if (i == 0) { if (new File(PATHS[i]).exists()) continue; pathList.add(Integer.valueOf(i)); } else { if (!new File(PATHS[i]).exists()) continue; pathList.add(Integer.valueOf(i)); } } if (pathList.size() == 0) { pathList = loadApps(paramContext); } return (pathList.size() == 0 ? null : pathList.toString()) != null; } catch (Exception e) { e.printStackTrace(); } return false; }
public static List getSimulatorInfo(Context paramContext) { List pathList = new ArrayList(); List simulatorMaps = new ArrayList(); try { pathList = getInstalledSimulatorPackages(paramContext); String brand = getSimulatorBrand(pathList); if (TextUtils.isEmpty(brand)) { List<String> list = loadApps(paramContext); if (list.size() > 0) { simulatorMaps.add(list.get(0)); } } else { simulatorMaps.add(brand); } } catch (Exception e) { e.printStackTrace(); } return simulatorMaps; } private static List getInstalledSimulatorPackages(Context context) { ArrayList localArrayList = new ArrayList(); try { for (int i = 0; i < PKG_NAMES.length; i++) try { context.getPackageManager().getPackageInfo(PKG_NAMES[i], 1); localArrayList.add(PKG_NAMES[i]); } catch (PackageManager.NameNotFoundException localNameNotFoundException) { } if (localArrayList.size() == 0) { for (int i = 0; i < FILES.length; i++) { if (new File(FILES[i]).exists()) localArrayList.add(FILES[i]); } } } catch (Exception e) { e.printStackTrace(); } return localArrayList; } public static List loadApps(Context context) { Intent intent = new Intent(Intent.ACTION_MAIN, null); intent.addCategory(Intent.CATEGORY_LAUNCHER); List<String> list = new ArrayList<>(); List<ResolveInfo> apps = context.getPackageManager().queryIntentActivities(intent, 0); //for循環遍歷ResolveInfo對象獲取包名和類名 for (int i = 0; i < apps.size(); i++) { ResolveInfo info = apps.get(i); String packageName = info.activityInfo.packageName; CharSequence cls = info.activityInfo.name; CharSequence name = info.activityInfo.loadLabel(context.getPackageManager()); if (!TextUtils.isEmpty(packageName)) { if (packageName.contains("bluestacks")) { list.add("藍疊"); return list; } } } return list; }
private static String getSimulatorBrand(List<String> list) { if (list.size() == 0) return ""; String pkgName = list.get(0); if (pkgName.contains("mumu")) { return "mumu"; } else if (pkgName.contains("ami")) { return "AMIDuOS"; } else if (pkgName.contains("bluestacks")) { return "藍疊"; } else if (pkgName.contains("kaopu001") || pkgName.contains("tiantian")) { return "每天"; } else if (pkgName.contains("kpzs")) { return "靠譜助手"; } else if (pkgName.contains("genymotion")) { if (Build.MODEL.contains("iTools")) { return "iTools"; } else if ((Build.MODEL.contains("ChangWan"))) { return "暢玩"; } else { return "genymotion"; } } else if (pkgName.contains("uc")) { return "uc"; } else if (pkgName.contains("blue")) { return "blue"; } else if (pkgName.contains("microvirt")) { return "逍遙"; } else if (pkgName.contains("itools")) { return "itools"; } else if (pkgName.contains("syd")) { return "手遊島"; } else if (pkgName.contains("bignox")) { return "夜神"; } else if (pkgName.contains("haimawan")) { return "海馬玩"; } else if (pkgName.contains("windroy")) { return "windroy"; } else if (pkgName.contains("flysilkworm")) { return "雷電"; } else if (pkgName.contains("emu")) { return "emu"; } else if (pkgName.contains("le8")) { return "le8"; } else if (pkgName.contains("vphone")) { return "vphone"; } else if (pkgName.contains("duoyi")) { return "多益"; } return ""; } }