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 collect sensor data and display by Android

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

Share

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

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!

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