传感器

Posted 钱大少丶

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了传感器相关的知识,希望对你有一定的参考价值。

 1 public class MainActivity extends AppCompatActivity implements SensorEventListener{
 2 
 3     private TextView text;
 4 
 5     @Override
 6     protected void onCreate(Bundle savedInstanceState) {
 7         super.onCreate(savedInstanceState);
 8         setContentView(R.layout.activity_main);
 9         initView();
10 
11         //获取传感器的管理
12         SensorManager sensor = (SensorManager) getSystemService(SENSOR_SERVICE);
13         List<Sensor> sensorList = sensor.getSensorList(Sensor.TYPE_ALL);
14         ArrayList<String> list = new ArrayList<>();
15         for (Sensor s:sensorList){
16             String str="\n"+"设备名称:"+s.getName()+"\n设备的版本:"+s.getVersion()+"\n供应商:"+s.getVendor();
17             list.add(str);
18         }
19         text.setText("经检测手机有"+list.size()+"个传感器,分别是:\n"+list.toString());
20 
21 
22         Sensor msensor1 = sensor.getDefaultSensor(Sensor.TYPE_GAME_ROTATION_VECTOR);
23         sensor.registerListener(this,msensor1,SensorManager.SENSOR_DELAY_GAME);
24 
25         Sensor msensor = sensor.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
26         //监听事件
27         sensor.registerListener(this,msensor,SensorManager.SENSOR_DELAY_GAME);
28 
29         Sensor light = sensor.getDefaultSensor(Sensor.TYPE_LIGHT);
30         sensor.registerListener(this,light,SensorManager.SENSOR_DELAY_GAME);
31 
32 
33     }
34 
35     private void initView() {
36         text = (TextView) findViewById(R.id.text);
37     }
38 
39 
40     @Override
41     public void onSensorChanged(SensorEvent event) {
42         int type = event.sensor.getType();
43         switch (type){
44 
45             case Sensor.TYPE_LIGHT:
46                 float[] values = event.values;
47                 StringBuilder builder = new StringBuilder();
48                 builder.append("光感度:"+values[0]);
49 //                builder.append("\ny方向的加速度"+values[1]);
50 //                builder.append("\nz方向的加速度:"+values[2]);
51                 text.setText(builder.toString());
52                 break;
53             case Sensor.TYPE_GAME_ROTATION_VECTOR:
54                 float[] values1 = event.values;
55                 StringBuilder builder1 = new StringBuilder();
56                 builder1.append("x方向的度量:"+values1[0]);
57                 builder1.append("\ny方向的度量:"+values1[1]);
58                 builder1.append("\nz方向的度量:"+values1[2]);
59                 text.setText(builder1.toString());
60                 break;
61         }
62 
63     }
64 
65     @Override
66     public void onAccuracyChanged(Sensor sensor, int accuracy) {
67 
68     }
69 }

 

以上是关于传感器的主要内容,如果未能解决你的问题,请参考以下文章

蓝桥杯——根据手册写底层

《实时控制软件》第二周作业

InputStreamReader抛出NullPointerException [duplicate]

Android:正确停止异步任务

微信小程序代码片段

VSCode自定义代码片段——CSS选择器