public class MainActivity extends AppCompatActivity implements SensorEventListener {
private long lastTime;
private ImageView up,down; private TextView textView,textView2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); //传感器的管理类 从系统中获取的 SensorManager sm = (SensorManager) getSystemService(Context.SENSOR_SERVICE); //从系统中获取传感器管理类中的全部管理器列表 List<Sensor> all = sm.getSensorList(Sensor.TYPE_ALL); List<String> list = new ArrayList<>(); //循环取出信息 for (Sensor s : all) { String tempString = "\n" + "设备名称:" + s.getName() + "\n" + "设备的版本:" + s.getVersion() + "\n" + "供应商:" + s.getVendor() + "\n"; list.add(tempString); } //显示传感器有多少个 textView.setText("经检测手机有" + all.size() + "个传感器,他们分别是:\n" + list.toString()); //注册监听事件 Sensor mSensor = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); sm.registerListener(this, mSensor, SensorManager.SENSOR_DELAY_GAME); Sensor mSensor1 = sm.getDefaultSensor(Sensor.TYPE_GEOMAGNETIC_ROTATION_VECTOR); sm.registerListener(this, mSensor1, SensorManager.SENSOR_DELAY_GAME); Sensor sensor = sm.getDefaultSensor(Sensor.TYPE_LIGHT); sm.registerListener(this, sensor, SensorManager. SENSOR_DELAY_NORMAL); } private void initView() { textView= (TextView) findViewById(R.id.textView); textView2= (TextView) findViewById(R.id.textView2); up= (ImageView) findViewById(R.id.imageView7); down= (ImageView) findViewById(R.id.imageView8); } @Override public void onSensorChanged(SensorEvent sensorEvent) { int type = sensorEvent.sensor.getType(); switch (type) { case Sensor.TYPE_ACCELEROMETER: float[] values = sensorEvent.values; StringBuilder sb = new StringBuilder(); //获取手机在不同方向上加速度的变化 // float valuesX = Math.abs(sensorEvent.values[0]); // float valuesY = Math.abs(sensorEvent.values[1]); // float valuesZ = Math.abs(sensorEvent.values[2]); sb.append("X方向的加速度:" + values[0]); sb.append("\nY方向的加速度:" + values[1]); sb.append("\nZ方向的加速度:" + values[2]); // if (valuesX > 17 || valuesY > 17 || valuesZ > 17) { // startAnimation(); // } textView.setText(sb); break; case Sensor.TYPE_LIGHT: float[] values3 = sensorEvent.values; StringBuilder sb3 = new StringBuilder(); sb3.append("光线的强度:" + values3[0]); textView2.setText(sb3); break; } } private void startAnimation() { //如果两次晃动手机的时间小于1秒,则只执行一次动画 long currentTimeMillis = System.currentTimeMillis(); if (currentTimeMillis - lastTime < 1000) { return; } lastTime = currentTimeMillis; AnimationSet upSet = new AnimationSet(true); TranslateAnimation upUp = new TranslateAnimation(TranslateAnimation.RELATIVE_TO_SELF, 0, TranslateAnimation.RELATIVE_TO_SELF, 0, TranslateAnimation.RELATIVE_TO_SELF, 0, TranslateAnimation.RELATIVE_TO_SELF, -1); upUp.setDuration(1000); TranslateAnimation upDown = new TranslateAnimation(TranslateAnimation.RELATIVE_TO_SELF, 0, TranslateAnimation.RELATIVE_TO_SELF, 0, TranslateAnimation.RELATIVE_TO_SELF, 0, TranslateAnimation.RELATIVE_TO_SELF, 1); upDown.setDuration(1000); upDown.setStartOffset(1000); upSet.addAnimation(upUp); upSet.addAnimation(upDown); up.startAnimation(upSet); AnimationSet downSet = new AnimationSet(true); TranslateAnimation downUp = new TranslateAnimation(TranslateAnimation.RELATIVE_TO_SELF, 0, TranslateAnimation.RELATIVE_TO_SELF, 0, TranslateAnimation.RELATIVE_TO_SELF, 0, TranslateAnimation.RELATIVE_TO_SELF, 1); downUp.setDuration(1000); TranslateAnimation downDown = new TranslateAnimation(TranslateAnimation.RELATIVE_TO_SELF, 0, TranslateAnimation.RELATIVE_TO_SELF, 0, TranslateAnimation.RELATIVE_TO_SELF, 0, TranslateAnimation.RELATIVE_TO_SELF, -1); downDown.setDuration(1000); downDown.setStartOffset(1000); downSet.addAnimation(downUp); downSet.addAnimation(downDown); down.startAnimation(downSet); } @Override public void onAccuracyChanged(Sensor sensor, int i) { }}
安卓传感器
最新推荐文章于 2024-05-25 10:46:38 发布