Android開發(fā)獲取傳感器數(shù)據(jù)的方法示例【加速度傳感器,磁場(chǎng)傳感器,光線傳感器,方向傳感器】
本文實(shí)例講述了Android開發(fā)獲取傳感器數(shù)據(jù)的方法。分享給大家供大家參考,具體如下:
package mobile.android.sensor;
import java.util.List;
import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;
public class Main extends Activity implements SensorEventListener {
private TextView tvAccelerometer;
private TextView tvMagentic;
private TextView tvLight;
private TextView tvOrientation;
private TextView tvSensors;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 獲得SensorManager對(duì)象
SensorManager sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
// 注冊(cè)加速度傳感器
sensorManager.registerListener(this,
sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_FASTEST);
// 注冊(cè)磁場(chǎng)傳感器
sensorManager.registerListener(this,
sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD),
SensorManager.SENSOR_DELAY_FASTEST);
// 注冊(cè)光線傳感器
sensorManager.registerListener(this,
sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT),
SensorManager.SENSOR_DELAY_FASTEST);
// 注冊(cè)方向傳感器
sensorManager.registerListener(this,
sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION),
SensorManager.SENSOR_DELAY_FASTEST);
tvAccelerometer = (TextView) findViewById(R.id.tvAccelerometer);
tvMagentic = (TextView) findViewById(R.id.tvMagentic);
tvLight = (TextView) findViewById(R.id.tvLight);
tvOrientation = (TextView) findViewById(R.id.tvOrientation);
tvSensors = (TextView) findViewById(R.id.tvSensors);
// 獲得當(dāng)前手機(jī)支持的所有傳感器
List<Sensor> sensors = sensorManager.getSensorList(Sensor.TYPE_ALL);
for (Sensor sensor : sensors) {
// 輸出當(dāng)前傳感器的名稱
tvSensors.append(sensor.getName() + "\n");
}
}
@Override
public void onSensorChanged(SensorEvent event) {
// 通過(guò)getType方法獲得當(dāng)前傳回?cái)?shù)據(jù)的傳感器類型
switch (event.sensor.getType()) {
case Sensor.TYPE_ACCELEROMETER: // 處理加速度傳感器傳回的數(shù)據(jù)
String accelerometer = "加速度\n" + "X:" + event.values[0] + "\n"
+ "Y:" + event.values[1] + "\n" + "Z:" + event.values[2]
+ "\n";
tvAccelerometer.setText(accelerometer);
break;
case Sensor.TYPE_LIGHT: // 處理光線傳感器傳回的數(shù)據(jù)
tvLight.setText("亮度:" + event.values[0]);
break;
case Sensor.TYPE_MAGNETIC_FIELD: // 處理磁場(chǎng)傳感器傳回的數(shù)據(jù)
String magentic = "磁場(chǎng)\n" + "X:" + event.values[0] + "\n" + "Y:"
+ event.values[1] + "\n" + "Z:" + event.values[2] + "\n";
tvMagentic.setText(magentic);
break;
case Sensor.TYPE_ORIENTATION: // 處理方向傳感器傳回的數(shù)據(jù)
String orientation = "方向\n" + "X:" + event.values[0] + "\n" + "Y:"
+ event.values[1] + "\n" + "Z:" + event.values[2] + "\n";
tvOrientation.setText(orientation);
break;
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
}
PS:這里獲得的只是傳感器采集數(shù)據(jù),還需要對(duì)數(shù)據(jù)分析,變成有用的信息。
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android開發(fā)入門與進(jìn)階教程》、《Android視圖View技巧總結(jié)》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android操作SQLite數(shù)據(jù)庫(kù)技巧總結(jié)》、《Android操作json格式數(shù)據(jù)技巧總結(jié)》、《Android資源操作技巧匯總》及《Android控件用法總結(jié)》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
相關(guān)文章
Android使用Xutils3進(jìn)行斷點(diǎn)下載的實(shí)例
在本篇內(nèi)容中小編給各位整理了關(guān)于Android使用Xutils3進(jìn)行斷點(diǎn)下載的實(shí)例以及相關(guān)代碼,需要的朋友們參考下。2019-07-07
Android基于OpenGL的GLSurfaceView創(chuàng)建一個(gè)Activity實(shí)現(xiàn)方法
這篇文章主要介紹了Android基于OpenGL的GLSurfaceView創(chuàng)建一個(gè)Activity實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了Android中OpenGL圖形操作類GLSurfaceView的功能、用法及相關(guān)使用技巧,需要的朋友可以參考下2016-10-10
Android?registerForActivityResult新用法實(shí)現(xiàn)兩個(gè)Activity間數(shù)據(jù)傳遞
這篇文章主要為大家介紹了Android?registerForActivityResult新用法實(shí)現(xiàn)兩個(gè)Activity間數(shù)據(jù)傳遞詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04
Android實(shí)現(xiàn)支付寶支付密碼輸入界面
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)支付寶支付密碼輸入界面,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05
Android自定義View實(shí)現(xiàn)價(jià)格區(qū)間選擇控件
這篇文章主要為大家詳細(xì)介紹了Android如何利用自定義View實(shí)現(xiàn)價(jià)格區(qū)間選擇控件,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以嘗試一下2022-11-11
Android 自定義View實(shí)現(xiàn)單擊和雙擊事件的方法
下面小編就為大家?guī)?lái)一篇Android 自定義View實(shí)現(xiàn)單擊和雙擊事件的方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-09-09

