Android传感器数据获取的方法
短信预约 -IT技能 免费直播动态提醒
本文实例为大家分享了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();
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程网。
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341