android识别模拟器与真机的一些方法

在某些场景中,我们需要识别用户使用的设备是模拟器还是真机,防止一些刷票,刷号等问题。

小的在网上也找了不少方法,但现在市面上的模拟器都太强大了,好多都不能用啊,所以就筛选了一些稍微靠谱一点的方法!(但不保证成功率啊毕竟没那么多机子去测试啊)

1.使用测试基带版本的方法。

百度百科:基带版本就是手机的调制解调器使用的驱动版本号,调制解调器主要目的负责着手机的通信功能(打电话,发短信,数据交换等)!

说白了就是手机能打电话发短信,有基带版本,模拟器不能插卡通信,没有基带版本。

代码片段如下:

@Override
protected void onResume() {
super.onResume();
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_BATTERY_CHANGED);
registerReceiver(mBroadcastReceiver, filter);
}


    @Override
protected void onPause() {
super.onPause();
unregisterReceiver(mBroadcastReceiver);
}
@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		version = (TextView) findViewById(R.id.version);
		try {

			mclass = Class.forName("android.os.SystemProperties");

			invoker = mclass.newInstance();

			mmethod = mclass.getMethod("get", new Class[] { String.class,String.class });

			result = mmethod.invoke(invoker, new Object[] {"gsm.version.baseband", "no message" });

			Log.i("基带版本:", (String) result);
			
			version.setText("基带版本:"+(String) result);

		} catch (Exception e) {

		}

	}

代码片段如上,经过测试,基本可以识别真机和模拟器,但问题来了,我的android平板没有基带版本啊!!因此如果是应用到了平板上,这种方法就不适用了。


下面介绍一种无法百分百成功的方法:

2.通过检测电池状态的方式

代码片段:

private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			String action = intent.getAction();
			if (action.equals(Intent.ACTION_BATTERY_CHANGED)) {
				int health = intent.getIntExtra("health", 0);
				show2 = (TextView) findViewById(R.id.show2);

				String healthString = "";
				switch (health) {
				case BatteryManager.BATTERY_HEALTH_UNKNOWN:
					healthString = "unknown";
					break;
				case BatteryManager.BATTERY_HEALTH_GOOD:
					healthString = "good";
					break;
				case BatteryManager.BATTERY_HEALTH_OVERHEAT:
					healthString = "overheat";
					break;
				case BatteryManager.BATTERY_HEALTH_DEAD:
					healthString = "dead";
					break;
				case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
					healthString = "voltage";
					break;
				case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE:
					healthString = "unspecified failure";
					break;
				}

				String s = "";
				s = "通过检测电池状态的方式:" + healthString;
				if (healthString.equals("unknown"))
					show2.setText(s + ",是模拟器");
				else
					show2.setText(s + ",不是模拟器");

			}
		}
	};

通过广播的方式监测电池状态,模拟器一般会返回unknown,因为它的没电池,不过这个好像会失败。


3.通过获取序列号的方式

这个序列号不是指IMEI,IMEI现在也能作假,本文中的序列号大家可以在设置--关于手机--状态消息--序列号里查看,我看了一些市面上比较流行的模拟器,发现都没有序列号,不过不知道这能不能造假,也许也能吧。

这种方法实现也比较简单,只需调用一句话就行,代码片段:

private void checkSimulator(){
		String SerialNumber = android.os.Build.SERIAL;//获取序列号
		show3 = (TextView) findViewById(R.id.show3);
		if (SerialNumber.equals("unknown"))
			show3.setText("通过获取序列号的方式:"+SerialNumber + ",是模拟器");
		else
			show3.setText("通过获取序列号的方式:"+SerialNumber + ",不是模拟器");

	}

4.通过检测是否有重力传感器的方式

重力传感器算是市面上android设备中标配的传感器,但是还是有很少一部分设备没有配备,可能是产品出的太早,因此这种方法也不是百分百能保证。

代码片段:

private void checkSimulator(){
		show1 = (TextView) findViewById(R.id.show1);
		sm = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
		s = sm.getDefaultSensor(Sensor.TYPE_GRAVITY);
		if (s == null)
			show1.setText("通过检测是否存在重力传感器的方式:是模拟器");
		else
			show1.setText("通过检测是否存在重力传感器的方式:不是模拟器");
	}

总结

对于模拟器的检测,参考了许多网上的方法,也实在无法找到完美的方案,也许只有各种检测方法相结合才行吧。


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值