package com.example.sensordemo;
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.util.Log;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import java.util.List;
public class MainActivity extends AppCompatActivity implements SensorEventListener {
final String TAG = MainActivity.class.getSimpleName();
private SensorManager sm;
TextView textView;
TextView textView2;
TextView textView3;
TextView textView4;
TextView textView5;
TextView textView6;
TextView textView7;
TextView textView8;
TextView textView9;
TextView textView10;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.textView);
textView2 = findViewById(R.id.textView2);
textView3 = findViewById(R.id.textView3);
textView4 = findViewById(R.id.textView4);
textView5 = findViewById(R.id.textView5);
textView6 = findViewById(R.id.textView6);
textView7 = findViewById(R.id.textView7);
textView8 = findViewById(R.id.textView8);
textView9 = findViewById(R.id.textView9);
textView10 = findViewById(R.id.textView10);
sm = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
registerSensor();
SensorManager mSensorManager;
List<Sensor> sensorList;
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); // 实例化传感器管理者
sensorList = mSensorManager.getSensorList(Sensor.TYPE_ALL);// 得到设置支持的所有传感器的List
for (Sensor sensor : sensorList) {
Log.d(TAG, "sensorNameList: \t" + sensor.getName());
}
}
@Override
public void onSensorChanged(SensorEvent event) {
Sensor sensor = event.sensor;
float x, y, z;
switch (sensor.getType()) {
case Sensor.TYPE_ACCELEROMETER:
x = event.values[0];
y = event.values[1];
z = event.values[2];
textView.setText("加速度传感器:\nx=" + x + "\ny=" + y + "\nz=" + z);
Log.d(TAG, "TYPE_ACCELEROMETER onSensorChanged: x=" + x + ",y=" + y + ",z=" + z);
break;
case Sensor.TYPE_MAGNETIC_FIELD:
x = event.values[0];
y = event.values[1];
z = event.values[2];
textView2.setText("磁场传感器:\nx=" + x + "\ny=" + y + "\nz=" + z);
Log.d(TAG, "TYPE_MAGNETIC_FIELD onSensorChanged: x=" + x + ",y=" + y + ",z=" + z);
break;
case Sensor.TYPE_ORIENTATION:
x = event.values[0];
y = event.values[1];
z = event.values[2];
textView3.setText("方向传感器:\nx=" + x + "\ny=" + y + "\nz=" + z);
Log.d(TAG, "TYPE_ORIENTATION onSensorChanged: x=" + x + ",y=" + y + ",z=" + z);
break;
case Sensor.TYPE_GYROSCOPE:
x = event.values[0];
y = event.values[1];
z = event.values[2];
textView4.setText("陀螺仪传感器:\nx=" + x + "\ny=" + y + "\nz=" + z);
Log.d(TAG, "TYPE_GYROSCOPE onSensorChanged: x=" + x + ",y=" + y + ",z=" + z);
break;
case Sensor.TYPE_GRAVITY:
x = event.values[0];
y = event.values[1];
z = event.values[2];
textView5.setText("重力仪传感器:\nx=" + x + "\ny=" + y + "\nz=" + z);
Log.d(TAG, "TYPE_GRAVITY onSensorChanged: x=" + x + ",y=" + y + ",z=" + z);
break;
case Sensor.TYPE_LINEAR_ACCELERATION:
x = event.values[0];
y = event.values[1];
z = event.values[2];
textView6.setText("线性加速度传感器:\nx=" + x + "\ny=" + y + "\nz=" + z);
Log.d(TAG, "TYPE_LINEAR_ACCELERATION onSensorChanged: x=" + x + ",y=" + y + ",z=" + z);
break;
case Sensor.TYPE_LIGHT:
x = event.values[0];
textView7.setText("光线传感器:\nx=" + x);
Log.d(TAG, "TYPE_LIGHT onSensorChanged: x=" + x);
Window window = this.getWindow();
WindowManager.LayoutParams lp = window.getAttributes();
lp.screenBrightness = (x > 100) ? 0.5f : 0f;//当前光线较强,手机亮度设为一半,反之最低。
window.setAttributes(lp);
break;
case Sensor.TYPE_PROXIMITY:
x = event.values[0];
textView8.setText("距离传感器:\nx=" + x);
Log.d(TAG, "TYPE_PROXIMITY onSensorChanged: x=" + x);
break;
case Sensor.TYPE_STEP_DETECTOR:
x = event.values[0];
textView9.setText("计步传感器d:\nx=" + x);
Log.d(TAG, "TYPE_STEP_DETECTOR onSensorChanged: x=" + x);
break;
case Sensor.TYPE_STEP_COUNTER:
x = event.values[0];
textView10.setText("计步传感器c:\nx=" + x);
Log.d(TAG, "TYPE_STEP_COUNTER onSensorChanged: x=" + x);
break;
default:
break;
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
@Override
protected void onStop() {
super.onStop();
sm.unregisterListener(this);
}
private void registerSensor() {
sm.registerListener(this, sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);
sm.registerListener(this, sm.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD), SensorManager.SENSOR_DELAY_NORMAL);
sm.registerListener(this, sm.getDefaultSensor(Sensor.TYPE_ORIENTATION), SensorManager.SENSOR_DELAY_NORMAL);
sm.registerListener(this, sm.getDefaultSensor(Sensor.TYPE_GYROSCOPE), SensorManager.SENSOR_DELAY_NORMAL);
sm.registerListener(this, sm.getDefaultSensor(Sensor.TYPE_GRAVITY), SensorManager.SENSOR_DELAY_NORMAL);
sm.registerListener(this, sm.getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION), SensorManager.SENSOR_DELAY_NORMAL);
sm.registerListener(this, sm.getDefaultSensor(Sensor.TYPE_LIGHT), SensorManager.SENSOR_DELAY_NORMAL);
sm.registerListener(this, sm.getDefaultSensor(Sensor.TYPE_PROXIMITY), SensorManager.SENSOR_DELAY_NORMAL);
sm.registerListener(this, sm.getDefaultSensor(Sensor.TYPE_STEP_COUNTER), SensorManager.SENSOR_DELAY_NORMAL);
sm.registerListener(this, sm.getDefaultSensor(Sensor.TYPE_STEP_DETECTOR), SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
protected void onResume() {
super.onResume();
registerSensor();
}
}
05-27
183
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交