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
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中展示系统的所有传感器列表
}