判斷Android是否為模擬器
阿新 • • 發佈:2019-01-04
package com.example.testemulator; import java.io.BufferedReader; import java.io.InputStreamReader; import android.content.Context; import android.content.Intent; import android.hardware.Sensor; import android.hardware.SensorManager; import android.net.Uri; import android.os.Build; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.telephony.TelephonyManager; import android.view.Menu; import android.view.MenuItem; import android.widget.TextView; public class MainActivity extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); boolean isEmulator = isEmulator(); TextView texta = (TextView)findViewById(R.id.a); if(isEmulator) { texta.setText("你是模擬器,逃不過我的法眼"); } else { texta.setText("很好,你是真機"); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } //是否有光感測器 private boolean hasLightSensorManager() { try{ SensorManager sm = (SensorManager)this.getSystemService(SENSOR_SERVICE); Sensor lightSensor = sm.getDefaultSensor(Sensor.TYPE_LIGHT); if(null == lightSensor) { return false; } else { return true; } } catch(Exception ex) { } return false; } //讀取cpu資訊 private String readCpuInfo() { String result = ""; try{ String[] args = {"/system/bin/cat","/proc/cpuinfo"}; ProcessBuilder cmd = new ProcessBuilder(args); Process process = cmd.start(); StringBuffer sb = new StringBuffer(); String readLine = ""; BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); while ((readLine = reader.readLine()) != null) { sb.append(readLine); } reader.close(); result = sb.toString().toLowerCase(); } catch(Exception ex) { } return result; } //判斷cpu是否是真機cpu private boolean checkIfIsPhoneCpu() { String cpuInfo = readCpuInfo(); if(cpuInfo.contains("intel") || cpuInfo.contains("amd")) { return false; } return true; } //判斷是否是模擬器 public boolean isEmulator() { String url = "tel:" + "123456"; Intent intent = new Intent(); intent.setData(Uri.parse(url)); intent.setAction(Intent.ACTION_DIAL); // 是否可以處理跳轉到撥號的 Intent boolean canResolveIntent = intent.resolveActivity(this.getPackageManager()) != null; return Build.FINGERPRINT.startsWith("generic") || Build.FINGERPRINT.toLowerCase().contains("vbox") || Build.FINGERPRINT.toLowerCase().contains("test-keys") || Build.MODEL.contains("google_sdk") || Build.MODEL.contains("Emulator") || Build.SERIAL.equalsIgnoreCase("unknown") || Build.SERIAL.equalsIgnoreCase("android") || 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) || ((TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE)) .getNetworkOperatorName().toLowerCase().equals("android") || !canResolveIntent || !hasLightSensorManager() || !checkIfIsPhoneCpu(); } }