得到传感器管理器
public class MainActivity extends AppCompatActivity {private SensorManager mSsystemService;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mSsystemService = (SensorManager) getSystemService(SENSOR_SERVICE);// 得到传感器管理器getAllSensor();private void getAllSensor() {List<Sensor> sensorList = mSsystemService.getSensorList(Sensor.TYPE_ALL);for (int i = 0; i < sensorList.size(); i++) {Sensor sensor = sensorList.get(i);String name = sensor.getName();String vendor = sensor.getVendor();// 得到生产厂商int version = sensor.getVersion();// 得到版本Log.d(TAG, \"onCreate:名字===== \" + name + \"====得到生产厂商-----\" + vendor + \"===得到版本=====\" + version);}}}
方向传感器
/** 方向传感器* 1. 获得传感器方法* 2. 创建传感器的监听器* 3.注册传感器。添加监听器* 4.在监听器的onSensorChanged方法中获取传感器感应到的数据,然后进行业务逻辑的处理** */userOre(); // 方向传感器private void userOre() {SensorManager mSsystemService = (SensorManager) getSystemService(SENSOR_SERVICE);Sensor otien = mSsystemService.getDefaultSensor(Sensor.TYPE_ORIENTATION);// 获取方向传感器mSsystemService.registerListener(new Lisenter(), otien, SensorManager.SENSOR_DELAY_NORMAL);// 注册// 方向传感器的监听public class Lisenter implements SensorEventListener {@Overridepublic void onSensorChanged(SensorEvent event) {float value = event.values[0];Log.d(TAG, \"onSensorChanged:方向角度 \" + value);}@Overridepublic void onAccuracyChanged(Sensor sensor, int accuracy) {}}}
光传感器的操作
getLight();//光传感器的操作SensorManager mSsystemService = (SensorManager) getSystemService(SENSOR_SERVICE);private void getLight() {Sensor sensor = mSsystemService.getDefaultSensor(Sensor.TYPE_LIGHT);mSsystemService.registerListener(lis2, sensor, SensorManager.SENSOR_DELAY_NORMAL);}//功能光传感器使用的监听器private SensorEventListener lis2 = new SensorEventListener() {@Overridepublic void onSensorChanged(SensorEvent event) {if (event.sensor.getType() == Sensor.TYPE_LIGHT) {float value = event.values[0];Log.d(TAG, \"onSensorLight: 光线强度\" + value);}}@Overridepublic void onAccuracyChanged(Sensor sensor, int accuracy) {}};
实现甩歌功能传感器
public class MainActivity extends AppCompatActivity {private SensorManager mSsystemService;private MediaPlayer mMp;private int[] musics = {R.raw.a, R.raw.b, R.raw.c};int index = 0;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mSsystemService = (SensorManager) getSystemService(SENSOR_SERVICE);/** 1.先获取加速度传感器* 2.创建相关的监听器--在 onSensorChanged 方法中编写* 3.注册* 4.运行甩手机测试* 5.创建Mediaplayer对象播放歌曲,甩的时候播放 准备歌曲仓库* */getAccele();//得到加速度传感器,实现甩歌功能private void getAccele() {//先播放音乐mMp = MediaPlayer.create(this, musics[index]);mMp.start();Sensor sensor = mSsystemService.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);//lis1监听器 sensor是感应器 SensorManager.SENSOR_DELAY_NORMAL感应器的灵敏度mSsystemService.registerListener(lis1, sensor, SensorManager.SENSOR_DELAY_NORMAL);//注册感应器的监听器}第一个传感器监听器,为加速度使用private SensorEventListener lis1 = new SensorEventListener() {@Overridepublic void onSensorChanged(SensorEvent event) {if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {//加速度感应器发生变化float[] value = event.values;//得到xyz三个方向的数据的变化float x = value[0];//x轴的新值float y = value[1];//y轴的新值float z = value[2];//z轴的新值Log.i(TAG, \"x=\" + x + \",y=\" + y + \",z=\" + z);if (z > 12) {//甩动切歌if (mMp != null) {mMp.stop();mMp.release();}index++;//下标加1index = index % musics.length;//防止越界mMp = MediaPlayer.create(MainActivity.this, musics[index]);mMp.start();}}}@Overridepublic void onAccuracyChanged(Sensor sensor, int accuracy) {}};}