码迷,mamicode.com
首页 > 其他好文 > 详细

传感器

时间:2017-11-23 10:52:52      阅读:124      评论:0      收藏:0      [点我收藏+]

标签:pat   system   cte   UI   his   tac   man   div   size   

 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 }

 

传感器

标签:pat   system   cte   UI   his   tac   man   div   size   

原文地址:http://www.cnblogs.com/SongYongQian/p/7883314.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!