当前位置:   article > 正文

android cocoscreator 检测模拟器还是真机_android studio判断模拟器还是真机

android studio判断模拟器还是真机

转载至 一行代码帮你检测Android模拟器   

具体原理看原博主文章,这里只讲cocoscreator3.6的安卓工程怎么使用

1.新建一个com.lahm.library包,和com.cocos.game同目录,如图示

那四个文件的代码如下:

EmulatorCheckUtil类,检测模拟器和真机的工具类,是入口:

  1. package com.lahm.library;
  2. import android.content.Context;
  3. import android.content.pm.PackageManager;
  4. import android.hardware.Sensor;
  5. import android.hardware.SensorManager;
  6. import android.text.TextUtils;
  7. import android.util.Log;
  8. import com.facebook.internal.LockOnGetVariable;
  9. import static android.content.Context.SENSOR_SERVICE;
  10. import static com.lahm.library.CheckResult.RESULT_EMULATOR;
  11. import static com.lahm.library.CheckResult.RESULT_MAYBE_EMULATOR;
  12. import static com.lahm.library.CheckResult.RESULT_UNKNOWN;
  13. /**
  14. * Project Name:检查是否是模拟器
  15. * Package Name:com.lahm.library
  16. * Created by lahm on 2018/6/8 15:01 .
  17. */
  18. public class EmulatorCheckUtil {
  19. private static String TAG = "EmulatorCheckUtil:";
  20. private EmulatorCheckUtil() {
  21. }
  22. private static class SingletonHolder {
  23. private static final EmulatorCheckUtil INSTANCE = new EmulatorCheckUtil();
  24. }
  25. public static final EmulatorCheckUtil getSingleInstance() {
  26. return SingletonHolder.INSTANCE;
  27. }
  28. public boolean readSysProperty(Context context, EmulatorCheckCallback callback) {
  29. if (context == null)
  30. throw new IllegalArgumentException("context must not be null");
  31. int suspectCount = 0;
  32. //检测硬件名称
  33. CheckResult hardwareResult = checkFeaturesByHardware();
  34. switch (hardwareResult.result) {
  35. case RESULT_MAYBE_EMULATOR:
  36. ++suspectCount;
  37. break;
  38. case RESULT_EMULATOR:
  39. if (callback != null) callback.findEmulator("hardware = " + hardwareResult.value);
  40. return true;
  41. }
  42. //检测渠道
  43. CheckResult flavorResult = checkFeaturesByFlavor();
  44. switch (flavorResult.result) {
  45. case RESULT_MAYBE_EMULATOR:
  46. ++suspectCount;
  47. break;
  48. case RESULT_EMULATOR:
  49. if (callback != null) callback.findEmulator("flavor = " + flavorResult.value);
  50. return true;
  51. }
  52. //检测设备型号
  53. CheckResult modelResult = checkFeaturesByModel();
  54. switch (modelResult.result) {
  55. case RESULT_MAYBE_EMULATOR:
  56. ++suspectCount;
  57. break;
  58. case RESULT_EMULATOR:
  59. if (callback != null) callback.findEmulator("model = " + modelResult.value);
  60. return true;
  61. }
  62. //检测硬件制造商
  63. CheckResult manufacturerResult = checkFeaturesByManufacturer();
  64. switch (manufacturerResult.result) {
  65. case RESULT_MAYBE_EMULATOR:
  66. ++suspectCount;
  67. break;
  68. case RESULT_EMULATOR:
  69. if (callback != null)
  70. callback.findEmulator("manufacturer = " + manufacturerResult.value);
  71. return true;
  72. }
  73. //检测主板名称
  74. CheckResult boardResult = checkFeaturesByBoard();
  75. switch (boardResult.result) {
  76. case RESULT_MAYBE_EMULATOR:
  77. ++suspectCount;
  78. break;
  79. case RESULT_EMULATOR:
  80. if (callback != null) callback.findEmulator("board = " + boardResult.value);
  81. return true;
  82. }
  83. //检测主板平台
  84. CheckResult platformResult = checkFeaturesByPlatform();
  85. switch (platformResult.result) {
  86. case RESULT_MAYBE_EMULATOR:
  87. ++suspectCount;
  88. break;
  89. case RESULT_EMULATOR:
  90. if (callback != null) callback.findEmulator("platform = " + platformResult.value);
  91. return true;
  92. }
  93. //检测基带信息
  94. CheckResult baseBandResult = checkFeaturesByBaseBand();
  95. switch (baseBandResult.result) {
  96. case RESULT_MAYBE_EMULATOR:
  97. suspectCount += 2;//模拟器基带信息为null的情况概率相当大
  98. break;
  99. case RESULT_EMULATOR:
  100. if (callback != null) callback.findEmulator("baseBand = " + baseBandResult.value);
  101. return true;
  102. }
  103. //检测传感器数量
  104. int sensorNumber = getSensorNumber(context);
  105. if (sensorNumber <= 7) ++suspectCount;
  106. //检测已安装第三方应用数量
  107. int userAppNumber = getUserAppNumber();
  108. if (userAppNumber <= 5) ++suspectCount;
  109. //检测是否支持闪光灯
  110. boolean supportCameraFlash = supportCameraFlash(context);
  111. if (!supportCameraFlash) ++suspectCount;
  112. //检测是否支持相机
  113. boolean supportCamera = supportCamera(context);
  114. if (!supportCamera) ++suspectCount;
  115. //检测是否支持蓝牙
  116. boolean supportBluetooth = supportBluetooth(context);
  117. if (!supportBluetooth) ++suspectCount;
  118. //检测光线传感器
  119. boolean hasLightSensor = hasLightSensor(context);
  120. if (!hasLightSensor) ++suspectCount;
  121. //检测进程组信息
  122. CheckResult cgroupResult = checkFeaturesByCgroup();
  123. if (cgroupResult.result == RESULT_MAYBE_EMULATOR) ++suspectCount;
  124. if (callback != null) {
  125. StringBuffer stringBuffer = new StringBuffer("Test start")
  126. .append("\r\n").append("hardware = ").append(hardwareResult.value)
  127. .append("\r\n").append("flavor = ").append(flavorResult.value)
  128. .append("\r\n").append("model = ").append(modelResult.value)
  129. .append("\r\n").append("manufacturer = ").append(manufacturerResult.value)
  130. .append("\r\n").append("board = ").append(boardResult.value)
  131. .append("\r\n").append("platform = ").append(platformResult.value)
  132. .append("\r\n").append("baseBand = ").append(baseBandResult.value)
  133. .append("\r\n").append("sensorNumber = ").append(sensorNumber)
  134. .append("\r\n").append("userAppNumber = ").append(userAppNumber)
  135. .append("\r\n").append("supportCamera = ").append(supportCamera)
  136. .append("\r\n").append("supportCameraFlash = ").append(supportCameraFlash)
  137. .append("\r\n").append("supportBluetooth = ").append(supportBluetooth)
  138. .append("\r\n").append("hasLightSensor = ").append(hasLightSensor)
  139. .append("\r\n").append("cgroupResult = ").append(cgroupResult.value)
  140. .append("\r\n").append("suspectCount = ").append(suspectCount);
  141. callback.findEmulator(stringBuffer.toString());
  142. }
  143. Log.i(TAG, "EmulatorCheck suspectCount:" + suspectCount);
  144. //嫌疑值大于3,认为是模拟器
  145. return suspectCount >= 3;
  146. }
  147. private int getUserAppNum(String userApps) {
  148. if (TextUtils.isEmpty(userApps)) return 0;
  149. String[] result = userApps.split("package:");
  150. return result.length;
  151. }
  152. private String getProperty(String propName) {
  153. String property = CommandUtil.getSingleInstance().getProperty(propName);
  154. return TextUtils.isEmpty(property) ? null : property;
  155. }
  156. /**
  157. * 特征参数-硬件名称
  158. *
  159. * @return 0表示可能是模拟器,1表示模拟器,2表示可能是真机
  160. */
  161. private CheckResult checkFeaturesByHardware() {
  162. String hardware = getProperty("ro.hardware");
  163. if (null == hardware) return new CheckResult(RESULT_MAYBE_EMULATOR, null);
  164. int result;
  165. String tempValue = hardware.toLowerCase();
  166. switch (tempValue) {
  167. case "ttvm"://天天模拟器
  168. case "nox"://夜神模拟器
  169. case "cancro"://网易MUMU模拟器
  170. case "intel"://逍遥模拟器
  171. case "vbox":
  172. case "vbox86"://腾讯手游助手
  173. case "android_x86"://雷电模拟器
  174. result = RESULT_EMULATOR;
  175. break;
  176. default:
  177. result = RESULT_UNKNOWN;
  178. break;
  179. }
  180. return new CheckResult(result, hardware);
  181. }
  182. /**
  183. * 特征参数-渠道
  184. *
  185. * @return 0表示可能是模拟器,1表示模拟器,2表示可能是真机
  186. */
  187. private CheckResult checkFeaturesByFlavor() {
  188. String flavor = getProperty("ro.build.flavor");
  189. if (null == flavor) return new CheckResult(RESULT_MAYBE_EMULATOR, null);
  190. int result;
  191. String tempValue = flavor.toLowerCase();
  192. if (tempValue.contains("vbox")) result = RESULT_EMULATOR;
  193. else if (tempValue.contains("sdk_gphone")) result = RESULT_EMULATOR;
  194. else result = RESULT_UNKNOWN;
  195. return new CheckResult(result, flavor);
  196. }
  197. /**
  198. * 特征参数-设备型号
  199. *
  200. * @return 0表示可能是模拟器,1表示模拟器,2表示可能是真机
  201. */
  202. private CheckResult checkFeaturesByModel() {
  203. String model = getProperty("ro.product.model");
  204. if (null == model) return new CheckResult(RESULT_MAYBE_EMULATOR, null);
  205. int result;
  206. String tempValue = model.toLowerCase();
  207. if (tempValue.contains("google_sdk")) result = RESULT_EMULATOR;
  208. else if (tempValue.contains("emulator")) result = RESULT_EMULATOR;
  209. else if (tempValue.contains("android sdk built for x86")) result = RESULT_EMULATOR;
  210. else result = RESULT_UNKNOWN;
  211. return new CheckResult(result, model);
  212. }
  213. /**
  214. * 特征参数-硬件制造商
  215. *
  216. * @return 0表示可能是模拟器,1表示模拟器,2表示可能是真机
  217. */
  218. private CheckResult checkFeaturesByManufacturer() {
  219. String manufacturer = getProperty("ro.product.manufacturer");
  220. if (null == manufacturer) return new CheckResult(RESULT_MAYBE_EMULATOR, null);
  221. int result;
  222. String tempValue = manufacturer.toLowerCase();
  223. if (tempValue.contains("genymotion")) result = RESULT_EMULATOR;
  224. else if (tempValue.contains("netease")) result = RESULT_EMULATOR;//网易MUMU模拟器
  225. else result = RESULT_UNKNOWN;
  226. return new CheckResult(result, manufacturer);
  227. }
  228. /**
  229. * 特征参数-主板名称
  230. *
  231. * @return 0表示可能是模拟器,1表示模拟器,2表示可能是真机
  232. */
  233. private CheckResult checkFeaturesByBoard() {
  234. String board = getProperty("ro.product.board");
  235. if (null == board) return new CheckResult(RESULT_MAYBE_EMULATOR, null);
  236. int result;
  237. String tempValue = board.toLowerCase();
  238. if (tempValue.contains("android")) result = RESULT_EMULATOR;
  239. else if (tempValue.contains("goldfish")) result = RESULT_EMULATOR;
  240. else result = RESULT_UNKNOWN;
  241. return new CheckResult(result, board);
  242. }
  243. /**
  244. * 特征参数-主板平台
  245. *
  246. * @return 0表示可能是模拟器,1表示模拟器,2表示可能是真机
  247. */
  248. private CheckResult checkFeaturesByPlatform() {
  249. String platform = getProperty("ro.board.platform");
  250. if (null == platform) return new CheckResult(RESULT_MAYBE_EMULATOR, null);
  251. int result;
  252. String tempValue = platform.toLowerCase();
  253. if (tempValue.contains("android")) result = RESULT_EMULATOR;
  254. else result = RESULT_UNKNOWN;
  255. return new CheckResult(result, platform);
  256. }
  257. /**
  258. * 特征参数-基带信息
  259. *
  260. * @return 0表示可能是模拟器,1表示模拟器,2表示可能是真机
  261. */
  262. private CheckResult checkFeaturesByBaseBand() {
  263. String baseBandVersion = getProperty("gsm.version.baseband");
  264. if (null == baseBandVersion) return new CheckResult(RESULT_MAYBE_EMULATOR, null);
  265. int result;
  266. if (baseBandVersion.contains("1.0.0.0")) result = RESULT_EMULATOR;
  267. else result = RESULT_UNKNOWN;
  268. return new CheckResult(result, baseBandVersion);
  269. }
  270. /**
  271. * 获取传感器数量
  272. */
  273. private int getSensorNumber(Context context) {
  274. SensorManager sm = (SensorManager) context.getSystemService(SENSOR_SERVICE);
  275. return sm.getSensorList(Sensor.TYPE_ALL).size();
  276. }
  277. /**
  278. * 获取已安装第三方应用数量
  279. */
  280. private int getUserAppNumber() {
  281. String userApps = CommandUtil.getSingleInstance().exec("pm list package -3");
  282. return getUserAppNum(userApps);
  283. }
  284. /**
  285. * 是否支持相机
  286. */
  287. private boolean supportCamera(Context context) {
  288. return context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA);
  289. }
  290. /**
  291. * 是否支持闪光灯
  292. */
  293. private boolean supportCameraFlash(Context context) {
  294. return context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
  295. }
  296. /**
  297. * 是否支持蓝牙
  298. */
  299. private boolean supportBluetooth(Context context) {
  300. return context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH);
  301. }
  302. /**
  303. * 判断是否存在光传感器来判断是否为模拟器
  304. * 部分真机也不存在温度和压力传感器。其余传感器模拟器也存在。
  305. *
  306. * @return false为模拟器
  307. */
  308. private boolean hasLightSensor(Context context) {
  309. SensorManager sensorManager = (SensorManager) context.getSystemService(SENSOR_SERVICE);
  310. Sensor sensor = sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT); //光线传感器
  311. if (null == sensor) return false;
  312. else return true;
  313. }
  314. /**
  315. * 特征参数-进程组信息
  316. */
  317. private CheckResult checkFeaturesByCgroup() {
  318. String filter = CommandUtil.getSingleInstance().exec("cat /proc/self/cgroup");
  319. if (null == filter) return new CheckResult(RESULT_MAYBE_EMULATOR, null);
  320. return new CheckResult(RESULT_UNKNOWN, filter);
  321. }
  322. }

 关键是 readSysProperty这个方法,返回false就不是模拟器是真机,true是模拟器

此包内的四个文件如下:
CheckResult.java
 

  1. package com.lahm.library;
  2. public class CheckResult {
  3. public static final int RESULT_MAYBE_EMULATOR = 0;//可能是模拟器
  4. public static final int RESULT_EMULATOR = 1;//模拟器
  5. public static final int RESULT_UNKNOWN = 2;//可能是真机
  6. public int result;
  7. public String value;
  8. public CheckResult(int result, String value) {
  9. this.result = result;
  10. this.value = value;
  11. }
  12. }
CommandUtil.java

  1. package com.lahm.library;
  2. import java.io.BufferedInputStream;
  3. import java.io.BufferedOutputStream;
  4. import java.io.IOException;
  5. /**
  6. * Project Name:EasyProtector
  7. * Package Name:com.lahm.library
  8. * Created by lahm on 2018/6/8 16:23 .
  9. */
  10. public class CommandUtil {
  11. private CommandUtil() {
  12. }
  13. private static class SingletonHolder {
  14. private static final CommandUtil INSTANCE = new CommandUtil();
  15. }
  16. public static final CommandUtil getSingleInstance() {
  17. return SingletonHolder.INSTANCE;
  18. }
  19. public String getProperty(String propName) {
  20. String value = null;
  21. Object roSecureObj;
  22. try {
  23. roSecureObj = Class.forName("android.os.SystemProperties")
  24. .getMethod("get", String.class)
  25. .invoke(null, propName);
  26. if (roSecureObj != null) value = (String) roSecureObj;
  27. } catch (Exception e) {
  28. value = null;
  29. } finally {
  30. return value;
  31. }
  32. }
  33. public String exec(String command) {
  34. BufferedOutputStream bufferedOutputStream = null;
  35. BufferedInputStream bufferedInputStream = null;
  36. Process process = null;
  37. try {
  38. process = Runtime.getRuntime().exec("sh");
  39. bufferedOutputStream = new BufferedOutputStream(process.getOutputStream());
  40. bufferedInputStream = new BufferedInputStream(process.getInputStream());
  41. bufferedOutputStream.write(command.getBytes());
  42. bufferedOutputStream.write('\n');
  43. bufferedOutputStream.flush();
  44. bufferedOutputStream.close();
  45. process.waitFor();
  46. String outputStr = getStrFromBufferInputSteam(bufferedInputStream);
  47. return outputStr;
  48. } catch (Exception e) {
  49. return null;
  50. } finally {
  51. if (bufferedOutputStream != null) {
  52. try {
  53. bufferedOutputStream.close();
  54. } catch (IOException e) {
  55. e.printStackTrace();
  56. }
  57. }
  58. if (bufferedInputStream != null) {
  59. try {
  60. bufferedInputStream.close();
  61. } catch (IOException e) {
  62. e.printStackTrace();
  63. }
  64. }
  65. if (process != null) {
  66. process.destroy();
  67. }
  68. }
  69. }
  70. private static String getStrFromBufferInputSteam(BufferedInputStream bufferedInputStream) {
  71. if (null == bufferedInputStream) {
  72. return "";
  73. }
  74. int BUFFER_SIZE = 512;
  75. byte[] buffer = new byte[BUFFER_SIZE];
  76. StringBuilder result = new StringBuilder();
  77. try {
  78. while (true) {
  79. int read = bufferedInputStream.read(buffer);
  80. if (read > 0) {
  81. result.append(new String(buffer, 0, read));
  82. }
  83. if (read < BUFFER_SIZE) {
  84. break;
  85. }
  86. }
  87. } catch (Exception e) {
  88. e.printStackTrace();
  89. }
  90. return result.toString();
  91. }
  92. }
EmulatorCheckCallback接口

  1. package com.lahm.library;
  2. /**
  3. * Project Name:EasyProtector
  4. * Package Name:com.lahm.library
  5. * Created by lahm on 2018/7/25 15:19 .
  6. */
  7. public interface EmulatorCheckCallback {
  8. void findEmulator(String emulatorInfo);
  9. }

在cocos.game.AppActivity这个类里,先声明一个静态变量
public static boolean isEnumator = false;

在onCreate(Bundle savedInstanceState)方法里这样调用:
this.isEnumator = EmulatorCheckUtil.getSingleInstance().readSysProperty(this, null);
这样就能检测出模拟器还是真机了,为true就是模拟器,false为真机。

再写个方法给typescript层调用:

  1. public static boolean isRunningOnEmulator() {
  2. return isEnumator;
  3. }

typescript层这样调用即可:
 

  1. getEmulator() { //检查是否是模拟器
  2. if(native.reflection) {
  3. global.bEmulator = native.reflection.callStaticMethod("com/cocos/game/AppActivity", "isRunningOnEmulator","()Z");
  4. }
  5. else
  6. global.bEmulator = false;
  7. }

大功告成,实测夜神模拟器,蓝叠模拟器能检测出来。

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

闽ICP备14008679号