android系统 传感器获取
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
android系统传感器获取
1显示系统获取的传感器的布局文件activity_main.xml
xmlns:tools="/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.shunchang.yingyong.test.cgq.MainActivity"> android:id="@+id/tips_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world"/> android:id="@+id/scrollView1" android:layout_below="@+id/tips_tv" android:layout_width="match_parent" android:layout_height="wrap_content"> android:id="@+id/all_ll" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical">
2获取系统的传感器:
setContentView(yout.activity_main);
all_ll=(LinearLayout)findViewById(R.id.all_ll);
tips_tv=(TextView)findViewById(R.id.tips_tv);
SensorManager sensorManager=(SensorManager)getSystemService(Contex
t.SENSOR_SERVICE);
List
tips_tv.setText("传感器的个数:"+ sensors.size());
for (Sensor s :sensors) {
Button bt=new Button(this);
bt.setText(getSensorName(s.getType()));
bt.setId(s.getType());
bt.setOnClickListener(this);
all_ll.addView(bt);
}
3根据传感器类型获取传感器的名称
private String getSensorName(int type){
String name="";
switch (type) {
case Sensor.TYPE_ACCELEROMETER:
name="重力加速度感测器";
break;
case Sensor.TYPE_MAGNETIC_FIELD:
name="磁场传感器";
break;
case Sensor.TYPE_ORIENTATION:
name="方位传感器";
break;
case Sensor.TYPE_GYROSCOPE:
name="陀螺仪传感器";
break;
case Sensor.TYPE_LIGHT:
name="环境光线传感器";
break;
case Sensor.TYPE_PRESSURE:
name="压力传感器";
break;
case Sensor.TYPE_TEMPERATURE:
name="温度传感器";
break;
case Sensor.TYPE_PROXIMITY:
name="距离传感器";
break;
case Sensor.TYPE_GRAVITY:
name="重力传感器";
break;
case Sensor.TYPE_LINEAR_ACCELERATION:
name="加速度传感器";
break;
case Sensor.TYPE_ROTATION_VECTOR:
name="矢量传感器";
break;
case Sensor.TYPE_RELATIVE_HUMIDITY:
name="湿度传感器";
break;
case Sensor.TYPE_AMBIENT_TEMPERATURE:
name="温度传感器";
break;
case Sensor.TYPE_MAGNETIC_FIELD_UNCALIBRATED: name="磁场传感器";
break;
case Sensor.TYPE_GAME_ROTATION_VECTOR:
name="旋转矢量传感器";
break;
case Sensor.TYPE_GYROSCOPE_UNCALIBRATED:
name="陀螺传感器";
break;
case Sensor.TYPE_SIGNIFICANT_MOTION:
name="运动触发传感器";
break;
case Sensor.TYPE_STEP_DETECTOR:
name="步探测器传感器";
break;
case Sensor.TYPE_STEP_COUNTER:
name="一步计数器传感器";
break;
case Sensor.TYPE_GEOMAGNETIC_ROTATION_VECTOR: name="地球磁旋转矢量";
break;
case Sensor.TYPE_HEART_RATE:
name="心率监测";
break;
// case Sensor.TYPE_TILT_DETECTOR:
// name="唤醒传感器";
// break;
// case Sensor.TYPE_WAKE_GESTURE:
// name="唤醒传感器";