欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

判断Android是否为模拟器

程序员文章站 2024-02-11 14:06:13
...

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();
        }

}

 

相关标签: Android 模拟器