本文实例为大家分享了Android传感器数据获取的具体代码,供大家参考,具体内容如下
接着上篇wifi列表获取,我们这篇接着说传感器。还是看代码
我们先自己定义一个传感器的工具类(因为我这边只是获取了部分的传感器,需要获取额外的传感器自己配置一下就行了,工具类通过重载提供了不同的构造方法):
public class SensorUtils implements SensorEventListener {
private SensorManager manager;
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);
}
}
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();
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程网。