您好,登录后才能下订单哦!
这篇文章主要讲解了“如何实现Android传感器数据获取”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“如何实现Android传感器数据获取”吧!
我们先自己定义一个传感器的工具类(因为我这边只是获取了部分的传感器,需要获取额外的传感器自己配置一下就行了,工具类通过重载提供了不同的构造方法):
public class SensorUtils implements SensorEventListener { private SensorManager manager; /** * @param context 多个传感器 * @param sensorList */ public void RegisterSensor(Context context, List<Sensor> sensorList){ manager = (SensorManager) context.getSystemService(SENSOR_SERVICE); for(Sensor sensors : sensorList){ manager.registerListener(this,sensors,SensorManager.SENSOR_DELAY_NORMAL); } } /** * @param context 单个传感器 * @param sensor */ public void RegisterSensor(Context context, Sensor sensor){ manager = (SensorManager) context.getSystemService(SENSOR_SERVICE); manager.registerListener(this,sensor,SensorManager.SENSOR_DELAY_NORMAL); } @Override public void onSensorChanged(SensorEvent event) { switch (event.sensor.getType()){ case Sensor.TYPE_ACCELEROMETER: //给加速度传感器设置回调监听 sensorCallBack.acceleratedCallBack(event); break; case Sensor.TYPE_GYROSCOPE: //给陀螺仪传感器设置回调监听 sensorCallBack.gyroscopeCallBack(event); break; case Sensor.TYPE_MAGNETIC_FIELD: //给磁场传感器设置回调监听 sensorCallBack.magneticFieldCallBack(event); break; case Sensor.TYPE_PRESSURE: //给气压计传感器设置回调监听 sensorCallBack.pressureCallBack(event); break; } } @Override public void onAccuracyChanged(Sensor sensor, int accuracy) { } //使用完之后记得取消注册 public void UnRegisterSensor(){ if (manager != null){ manager.unregisterListener(this); } } public interface SensorCallBack{ void acceleratedCallBack(SensorEvent event); void gyroscopeCallBack(SensorEvent event); void magneticFieldCallBack(SensorEvent event); void pressureCallBack(SensorEvent event); } private SensorCallBack sensorCallBack; public SensorUtils(SensorCallBack sensorCallBack){ this.sensorCallBack = sensorCallBack; } }
然后我们在activity中去使用工具类:
//初始化 sensorUtils = new SensorUtils(this); sensorUtils.RegisterSensor(this, sensorList);
activity中别忘了实现SensorUtil中的回调监听
public class SensorActivity extends BaseActivity<BaseViewModel, ActivitySensorBinding> implements SensorUtils.SensorCallBack{ //do something }
然后在自己设置的回调监听中去实现自己想要的功能
@Override public void acceleratedCallBack(SensorEvent event) { //可以做自己想要的操作 if (event != null) { float[] values = event.values; float x1 = values[0]; float y1 = values[1]; float z1 = values[2]; StringBuffer stringBuffer = new StringBuffer(); stringBuffer.append(getString(R.string.accelerometer_number)).append("\n"); stringBuffer.append(getString(R.string.x1)).append(x1).append("\n"); stringBuffer.append(getString(R.string.y1)).append(y1).append("\n"); stringBuffer.append(getString(R.string.z1)).append(z1); if (!viewDateBinding.scrollView.isMove()) { handler.post(() -> viewDateBinding.tvAccelerated.setText(stringBuffer.toString())); } if (flagAccelerated) { DataBassManager.getInstance(SensorActivity.this).insertSensor(new TableBean(null,stringBuffer.toString(),null,null,null)); } flagAccelerated = false; } } @Override public void gyroscopeCallBack(SensorEvent event) { //可以做自己想要的操作 if (event != null) { float x2 = event.values[0]; float y2 = event.values[1]; float z2 = event.values[2]; StringBuffer stringBuffer = new StringBuffer(); stringBuffer.append(getString(R.string.gyroscope_number)).append("\n"); stringBuffer.append(getString(R.string.x2)).append(x2).append("\n"); stringBuffer.append(getString(R.string.y2)).append(y2).append("\n"); stringBuffer.append(getString(R.string.z2)).append(z2); if (!viewDateBinding.scrollView.isMove()) { handler.post(() -> viewDateBinding.tvGyroscope.setText(stringBuffer.toString())); } if (flagGyroscope) { DataBassManager.getInstance(SensorActivity.this).insertSensor(new TableBean(null,null,stringBuffer.toString(),null,null)); } flagGyroscope = false; } } @SuppressLint("DefaultLocale") @Override public void magneticFieldCallBack(SensorEvent event) { //可以做自己想要的操作 if (event != null) { float x3 = event.values[0]; float y3 = event.values[1]; float z3 = event.values[2]; StringBuffer stringBuffer = new StringBuffer(); stringBuffer.append(getString(R.string.magnetic_field_number)).append("\n"); stringBuffer.append(getString(R.string.x3)).append(String.format("%.2f", x3)).append("\n"); stringBuffer.append(getString(R.string.y3)).append(String.format("%.2f", y3)).append("\n"); stringBuffer.append(getString(R.string.z3)).append(String.format("%.2f", z3)); if (!viewDateBinding.scrollView.isMove()) { handler.post(() -> viewDateBinding.tvMagneticField.setText(stringBuffer.toString())); } if (flagMagneticField) { DataBassManager.getInstance(SensorActivity.this).insertSensor(new TableBean(null,null,null,stringBuffer.toString(),null)); } flagMagneticField = false; } } @Override public void pressureCallBack(SensorEvent event) { //可以做自己想要的操作 if (event != null) { float x4 = event.values[0]; String str4 = getString(R.string.pressure_number) + x4; if (!viewDateBinding.scrollView.isMove()) { handler.post(() -> viewDateBinding.tvPressure.setText(str4)); } if (flagPressure) { DataBassManager.getInstance(SensorActivity.this).insertSensor(new TableBean(null,null,null,null,str4)); } flagPressure = false; } }
我这边是将数据通过按钮的点击事件存储进了数据库,注意传感器的回调是实时的,所以如果想要获取某一时刻,需要自己通过boolean去判断是否需要获取回调数据
最后,记得取消监听,因为我是在activity中去注册的,所以取消注册也是在activity中去做:
protected void onStop() { super.onStop(); sensorUtils.UnRegisterSensor(); }
感谢各位的阅读,以上就是“如何实现Android传感器数据获取”的内容了,经过本文的学习后,相信大家对如何实现Android传感器数据获取这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。