获取系统传感器

来源:互联网 发布:以太网是什么网络 编辑:程序博客网 时间:2024/06/05 08:58
1.控件Layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/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"    >    <TextView        android:id="@+id/tv"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="Hello World!" />
2.Activity里
public class MainActivity extends AppCompatActivity {    TextView tv;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        tv=(TextView) findViewById(R.id.tv);
//获取传感器管理
TYPE_ACCELEROMETER         //加速度传感器(硬件)TYPE_MAGNETIC_FIELD        //磁场传感器(硬件)TYPE_ORIENTATION           //方向传感器(软件传感器,数据来自重力和磁场传感器)TYPE_GYROSCOPE             //陀螺仪传感器(硬件)TYPE_LIGHT                 //光线传感器(硬件)TYPE_PRESSURE              //压力传感器(硬件)TYPE_PROXIMITY             //临近传感器(硬件)TYPE_GRAVITY               //重场传感器(硬件或软件)TYPE_LINEAR_ACCELERATION   //线性加速度传感器(硬件或软件)TYPE_ROTATION_VECTOR       //旋转矢量传感器(硬件或软件)TYPE_RELATIVE_HUMIDITY     //湿度传感器(硬件)TYPE_AMBIENT_TEMPERATURE   //温度传感器(硬件)TYPE_TEMPERATURE           //温度传感器(硬件),从Android4.0开始被TYPE_AMBIENT_TEMPERATURE取代
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
    SensorManager manager = (SensorManager) getSystemService(SENSOR_SERVICE); List<Sensor> sensotList=manager.getSensorList(Sensor.TYPE_ALL); for (Sensor sensor:sensotList){
                tv.append("\r\n"+sensor.getName());//tv中展示系统的所有传感器列表        }

    原创粉丝点击