Get the App
SLTechnology News&Howtos  ›  Development  › 

How to collect sensor data and display by Android

Shulou Source: shulou.com Published: 2022-06-03 03:57:18 10月02日 Update

This article is about how Android collects sensor data and displays it. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

The knowledge needed

Android project main configuration file AndroidManifest.xml

Android Activity concept

Android Sensor interface

Android UI layout Manager, text Box component, Edit Box component

Code and interpretation

1. Layout file, used to control the UI interface display, here the use of a table layout manager and four rows, each row has four columns, of which the outer two columns are to center the middle two columns, while the middle two columns are a TextView text box component that shows the data source and an edit box component that displays the data values.

two。 Program file, which is used to collect sensor data and control its display

There is a lot of information about Android Activity, it is said here that it is a page of an Android program and its corresponding execution content; Activity's life flow, onCreate (), onStart (), onResume () …

There are three steps to collect sensor data, first get a sensor manager object, then register the listener, and finally determine whether the sensor type is needed when the sensor value changes, and then make a refresh or other response.

/ / MainActivity.javapackage com.example.myapplication;import androidx.appcompat.app.AppCompatActivity;import android.content.Context;import android.hardware.Sensor;import android.hardware.SensorEvent;import android.hardware.SensorEventListener;import android.hardware.SensorManager;import android.os.Bundle;import android.widget.EditText;public class MainActivity extends AppCompatActivity implements SensorEventListener {/ / implement SensorEventListener interface private EditText textAcceX,textAcceY,textAcceZ; / / edit box component private SensorManager sensorManager / / Sensor Manager component @ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.activity_main); textAcceX = findViewById (R.id.et1); textAcceY = findViewById (R.id.et2); textAcceZ = findViewById (R.id.et3); sensorManager = (SensorManager) getSystemService (Context.SENSOR_SERVICE) / / get sensor manager} @ Override protected void onResume () {super.onResume (); / / set sensor type and sampling rate sensorManager.registerListener (this, sensorManager.getDefaultSensor (Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_UI);} @ Override protected void onPause () {super.onPause (); sensorManager.unregisterListener (this) / / suspend collection} @ Override public void onSensorChanged (SensorEvent event) {/ / rewrite the method of SensorEventListener interface float [] values = event.values; int sensorType = event.sensor.getType (); StringBuilder stringBuilderX = null,stringBuilderY = null,stringBuilderZ = null; if (sensorType = = Sensor.TYPE_ACCELEROMETER) {/ / determine whether the sensor is required stringBuilderX = new StringBuilder () StringBuilderY = new StringBuilder (); stringBuilderZ = new StringBuilder (); stringBuilderX.append (values [0]); stringBuilderY.append (values [1]); stringBuilderZ.append (values [2]); textAcceX.setText (stringBuilderX.toString ()); / / display textAcceY.setText (stringBuilderY.toString ()) in the edit box TextAcceZ.setText (stringBuilderZ.toString ());} @ Override public void onAccuracyChanged (Sensor sensor, int accuracy) {/ / rewrite the method of the SensorEventListener interface}} Thank you for reading! This is the end of the article on "how Android collects sensor data and displays it". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Tags: Sensors data components management interfaces content layout files text methods more knowledge programs articles types controls good practical one line Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB macOS Microsoft vpn MySQL