Android 如何获取传感器的数据方法详解
public class MainActivity extends AppCompatActivity {
private final String TAG = "sensor-sample";
private TextView mAccelerometerSensorTextView;
private TextView mMagneticSensorTextView;
private TextView mGyroscopeSensorTextView;
private TextView mOrientationSensorTextView;
private SensorManager mSensorManager;
private MySensorEventListener mMySensorEventListener;
private float[] mAccelerometerReading = new float[3];
private float[] mMagneticFieldReading = new float[3];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAccelerometerSensorTextView = findViewById(R.id.accelerometer_sensor);
mMagneticSensorTextView = findViewById(R.id.magnetic_sensor);
mGyroscopeSensorTextView = findViewById(R.id.gyroscope_sensor);
mOrientationSensorTextView = findViewById(R.id.orientation_sensor);
this.mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
this.mMySensorEventListener = new MySensorEventListener();
}
@Override
protected void onResume() {
super.onResume();
if (mSensorManager == null) {
return;
}
Sensor accelerometerSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
if (accelerometerSensor != null) {
//register accelerometer sensor listener
mSensorManager.registerListener(mMySensorEventListener, accelerometerSensor, SensorManager.SENSOR_DELAY_UI);
} else {
Log.d(TAG, "Accelerometer sensors are not supported on current devices.");
}
Sensor magneticSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
if (magneticSensor != null) {
//register magnetic sensor listener
mSensorManager.registerListener(mMySensorEventListener, magneticSensor, SensorManager.SENSOR_DELAY_UI);
} else {
Log.d(TAG, "Magnetic sensors are not supported on current devices.");
}
Sensor gyroscopeSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
if (gyroscopeSensor != null) {
//register gyroscope sensor listener
mSensorManager.registerListener(mMySensorEventListener, gyroscopeSensor, SensorManager.SENSOR_DELAY_UI);
} else {
Log.d(TAG, "Gyroscope sensors are not supported on current devices.");
}
}
@Override
protected void onPause() {
super.onPause();
if (mSensorManager == null) {
return;
}
//unregister all listener
mSensorManager.unregisterListener(mMySensorEventListener);
}
/*
This orientation sensor was deprecated in Android 2.2 (API level 8), and this sensor type was deprecated in Android 4.4W (API level 20).
The sensor framework provides alternate methods for acquiring device orientation.
*/
private void calculateOrientation() {
final float[] rotationMatrix = new float[9];
SensorManager.getRotationMatrix(rotationMatrix, null, mAccelerometerReading, mMagneticFieldReading);
final float[] orientationAngles = new float[3];
SensorManager.getOrientation(rotationMatrix, orientationAngles);
Log.d(TAG, "orientation data[x:" + orientationAngles[0] + ", y:" + orientationAngles[1] + ", z:" + orientationAngles[2] + "]");
mOrientationSensorTextView.setText("[x:" + orientationAngles[0] + ", y:" + orientationAngles[1] + ", z:" + orientationAngles[2] + "]");
}
private class MySensorEventListener implements SensorEventListener {
@Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
mAccelerometerReading = event.values;
Log.d(TAG, "accelerometer data[x:" + event.values[0] + ", y:" + event.values[1] + ", z:" + event.values[2] + "]");
mAccelerometerSensorTextView.setText("[x:" + event.values[0] + ", y:" + event.values[1] + ", z:" + event.values[2] + "]");
} else if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
mMagneticFieldReading = event.values;
Log.d(TAG, "magnetic data[x:" + event.values[0] + ", y:" + event.values[1] + ", z:" + event.values[2] + "]");
mMagneticSensorTextView.setText("[x:" + event.values[0] + ", y:" + event.values[1] + ", z:" + event.values[2] + "]");
} else if (event.sensor.getType() == Sensor.TYPE_GYROSCOPE) {
Log.d(TAG, "gyroscope data[x:" + event.values[0] + ", y:" + event.values[1] + ", z:" + event.values[2] + "]");
mGyroscopeSensorTextView.setText("[x:" + event.values[0] + ", y:" + event.values[1] + ", z:" + event.values[2] + "]");
}
calculateOrientation();
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
Log.d(TAG, "onAccuracyChanged:" + sensor.getType() + "->" + accuracy);
}
}
}
- .NET Core系列之MemoryCache 初识
- 007手机一键Root(安机网一键Root) v3.0 官方最新版 一键ROOT您的Android手机
- 12306密码被盗了怎么办?12306密码外泄解决方法
- 12个字的qq网名
- 150M迷你型无线路由器怎么设置?
- 192.168.1.1打不开怎么办?路由器192.168.1.1打不开的原因以及解决办法
- 2011年电子报合订本 电子报 编辑部 中文 PDF版 [84M]
- 2015年1月15日小米新旗舰发布会现场图文直播
- 2016.3.1vivo Xplay5新品发布会现场视频直播 优酷直播
- 2016华为P9发布会视频直播地址 4月15日华为P9国行发布会直播