Android采集傳感器數(shù)據(jù)并顯示的方法
本文實(shí)例為大家分享了Android采集傳感器數(shù)據(jù)并顯示的具體代碼,供大家參考,具體內(nèi)容如下
需要的知識(shí)
- Android 項(xiàng)目主配置文件 AndroidManifest.xml
- Android Activity 概念
- Android Sensor 接口
- Android UI 布局管理器、文本框組件、編輯框組件
代碼及解釋
1.布局文件,用于控制UI界面顯示,這里使用了表格布局管理器和四個(gè)行,每一行之中有四列,其中外面的兩列是為了讓中間兩列居中,而中間兩列分別是一個(gè)顯示數(shù)據(jù)來源的TextView文本框組件和一個(gè)顯示數(shù)據(jù)值的編輯框組件。
<!-- activity_main.xml -->
<?xml version="1.0" encoding="utf-8"?>
<!-- 表格布局管理器TableLayout -->
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp"
android:stretchColumns="0,3"
tools:context=".MainActivity" >
<!--三行,傳感器數(shù)據(jù)顯示-->
<TableRow android:paddingTop="10dp">
<!--文本框組件-->
<TextView />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="x軸加速度:"
android:textSize="18sp"
android:gravity="center_horizontal"/>
<!--編輯框組件-->
<EditText
android:id="@+id/et1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="000"/>
<TextView />
</TableRow>
<TableRow >
<TextView />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="y軸加速度:"
android:textSize="18sp"
android:gravity="center_horizontal"/>
<EditText
android:id="@+id/et2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="000"/>
<TextView />
</TableRow>
<TableRow >
<TextView />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="z軸加速度:"
android:textSize="18sp"
android:gravity="center_horizontal"/>
<EditText
android:id="@+id/et3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="000"/>
<TextView />
</TableRow>
</TableLayout>
2.程序文件,用于采集傳感器數(shù)據(jù)并控制其顯示
關(guān)于 Android Activity 有很多資料,這里就說其是一個(gè) Android 程序的一個(gè)頁(yè)面及其對(duì)應(yīng)的執(zhí)行內(nèi)容;Activity 的生命流程,onCreate( ),onStart( ),onResume( )…

采集傳感器數(shù)據(jù)有三步,首先獲取一個(gè)傳感器管理器對(duì)象,然后注冊(cè)監(jiān)聽器,最后判斷傳感器值改變時(shí)是否需要的傳感器類型,然后做出刷新或其它反應(yīng);
// MainActivity.java
package 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 { // 實(shí)現(xiàn)Sensor Event Listener接口
private EditText textAcceX,textAcceY,textAcceZ; //編輯框組件
private SensorManager sensorManager; //傳感器管理器組件
@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); // 獲取傳感器管理器
}
@Override
protected void onResume() {
super.onResume();
//設(shè)置傳感器類型及采樣率
sensorManager.registerListener(this,
sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),SensorManager.SENSOR_DELAY_UI);
}
@Override
protected void onPause() {
super.onPause();
sensorManager.unregisterListener(this); // 暫停采集
}
@Override
public void onSensorChanged(SensorEvent event) { // 重寫SensorEventListener接口的方法
float [] values = event.values;
int sensorType = event.sensor.getType();
StringBuilder stringBuilderX = null,stringBuilderY = null,stringBuilderZ = null;
if(sensorType == Sensor.TYPE_ACCELEROMETER ){ // 判斷是否所需傳感器
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()); // 編輯框內(nèi)顯示
textAcceY.setText(stringBuilderY.toString());
textAcceZ.setText(stringBuilderZ.toString());
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) { // 重寫SensorEventListener接口的方法
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
android實(shí)現(xiàn)注冊(cè)頁(yè)面開發(fā)
這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)注冊(cè)頁(yè)面開發(fā),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04
Kotlin整合Vertx開發(fā)Web應(yīng)用
這篇文章主要介紹了Kotlin整合Vertx開發(fā)Web應(yīng)用,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-02-02
Android 滾動(dòng)時(shí)間選擇的示例代碼
這篇文章主要介紹了Android 滾動(dòng)時(shí)間選擇的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12
Android內(nèi)存溢出及內(nèi)存泄漏原因進(jìn)解析
這篇文章主要介紹了Android內(nèi)存溢出及內(nèi)存泄漏原因解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08
Android 菜單欄DIY實(shí)現(xiàn)效果詳解
這篇文章主要為大家介紹了Android 菜單欄DIY實(shí)現(xiàn)效果詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09
詳解基于Android App 安全登錄認(rèn)證解決方案
這篇文章主要介紹了基于Android App 安全登錄認(rèn)證解決方案,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-09-09

