Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize data acquisition of Android Sensor

2025-02-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "how to achieve Android sensor data acquisition". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to achieve Android sensor data acquisition".

Let's first define a tool class for the sensor (because I only get part of the sensor here, we need to get additional sensors to configure ourselves, and the tool class provides different construction methods through overloading):

Public class SensorUtils implements SensorEventListener {private SensorManager manager; / * @ param context multiple sensors * @ param sensorList * / public void RegisterSensor (Context context, List sensorList) {manager = (SensorManager) context.getSystemService (SENSOR_SERVICE); for (Sensor sensors: sensorList) {manager.registerListener (this,sensors,SensorManager.SENSOR_DELAY_NORMAL) }} / * @ param context single sensor * @ 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: / / set callback listener sensorCallBack.acceleratedCallBack (event) for accelerometer; break Case Sensor.TYPE_GYROSCOPE: / / set callback listening sensorCallBack.gyroscopeCallBack (event) for gyroscope sensor; break; case Sensor.TYPE_MAGNETIC_FIELD: / / set callback listening sensorCallBack.magneticFieldCallBack (event) for magnetic field sensor; break Case Sensor.TYPE_PRESSURE: / / set callback listening sensorCallBack.pressureCallBack (event) for barometer sensor; break } @ Override public void onAccuracyChanged (Sensor sensor, int accuracy) {} / / remember to unregister 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;}}

Then we use the utility class in activity:

/ / initialize sensorUtils = new SensorUtils (this); sensorUtils.RegisterSensor (this, sensorList)

Don't forget to implement callback listening in SensorUtil in activity

Public class SensorActivity extends BaseActivity implements SensorUtils.SensorCallBack {/ / do something}

Then implement the desired function in the callback monitor set by yourself.

@ Override public void acceleratedCallBack (SensorEvent event) {/ / you can do the operation you want 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) {/ / you can do the operation you want 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) {/ / you can do the operation you want 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) {/ / you can do the operation you want 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;}}

On my side, the data is stored in the database through the click event of the button. Note that the callback of the sensor is real-time, so if you want to get a certain moment, you need to use boolean to determine whether you need to obtain callback data.

Finally, remember to unlisten, because I registered in activity, so unregistering is also done in activity:

Protected void onStop () {super.onStop (); sensorUtils.UnRegisterSensor ();} Thank you for your reading. The above is the content of "how to achieve Android sensor data acquisition". After the study of this article, I believe you have a deeper understanding of how to achieve Android sensor data acquisition, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report