android手機內有多個傳感器,好比加速度計、陀螺儀等。java
本篇介紹如何獲取加速度數據並顯示到TextView上。android
1、想要達到的效果
功能:app
一、點擊開始按鈕時,啓動加速度傳感器,讀取數據,顯示到屏幕上;ide
二、點擊中止按鈕,中止加速度傳感器。this
知識點:spa
一、一個activity怎樣實現兩個接口3d
二、怎樣得到傳感器服務,註冊服務,註銷服務code
三、怎樣得到button、textview句柄,以便操做xml
四、怎樣關聯button的click事件blog
2、activity文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <TextView android:id="@+id/tvx" android:layout_width="214dp" android:layout_height="69dp" android:text="TextView" tools:layout_editor_absoluteX="117dp" tools:layout_editor_absoluteY="100dp" /> <TextView android:id="@+id/tvy" android:layout_width="214dp" android:layout_height="53dp" android:text="TextView" tools:layout_editor_absoluteX="126dp" tools:layout_editor_absoluteY="158dp" /> <TextView android:id="@+id/tvz" android:layout_width="214dp" android:layout_height="53dp" android:text="TextView" tools:layout_editor_absoluteX="130dp" tools:layout_editor_absoluteY="234dp" /> <Button android:id="@+id/bt_dsp" android:layout_width="131dp" android:layout_height="79dp" android:text="開始顯示加速度" tools:layout_editor_absoluteX="115dp" tools:layout_editor_absoluteY="444dp" /> <Button android:id="@+id/bt_stop" android:layout_width="217dp" android:layout_height="81dp" android:text="中止顯示加速度" /> </LinearLayout>
3、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.view.View; import android.widget.Button; import android.widget.TextView; public class MainActivity extends AppCompatActivity implements SensorEventListener,View.OnClickListener { private SensorManager mSensorMgr; private TextView tvx; private TextView tvy; private TextView tvz; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button bt=findViewById(R.id.bt_dsp); bt.setOnClickListener(this); Button bt_stop=findViewById(R.id.bt_stop); bt_stop.setOnClickListener(this); tvx=findViewById(R.id.tvx); tvy=findViewById(R.id.tvy); tvz=findViewById(R.id.tvz); // mSensorMgr=(SensorManager)getSystemService(Context.SENSOR_SERVICE); } protected void onPause() { super.onPause(); mSensorMgr.unregisterListener(this); } protected void onResume() { super.onResume(); } protected void onStop() { super.onStop(); mSensorMgr.unregisterListener(this); } public void onSensorChanged(SensorEvent event) { if(event.sensor.getType()==Sensor.TYPE_ACCELEROMETER) { float[] values=event.values; tvx.setText("ACC_X: "+Float.toString(values[0])); tvy.setText("ACC_Y: "+Float.toString(values[1])); tvz.setText("ACC_Z: "+Float.toString(values[2])); } } public void onAccuracyChanged(Sensor sensor,int accuracy) {//不用處理,空着就行 return; } public void onClick(View v) { if(v.getId()==R.id.bt_dsp) { mSensorMgr.unregisterListener(this,mSensorMgr.getDefaultSensor(Sensor.TYPE_ACCELEROMETER)); mSensorMgr.registerListener(this, mSensorMgr.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL); return; } if(v.getId()==R.id.bt_stop) { mSensorMgr.unregisterListener(this); return; } } }