不多说,先上代码再配图。
1、建立android项目
2、修改布局layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="有哪些传感器?"
/>
<TextView
android:id="@+id/sensor_list"
android:text=""
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
3、修改java代码
package com.example.HelloWorld;
import android.app.Activity;
import android.os.Bundle;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.widget.TextView;
import java.util.List;
public class MyActivity extends Activity{
private SensorManager sensorManager;
TextView sensorList;
private String sensorsInMyPhone = "";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
sensorList=(TextView)findViewById(R.id.sensor_list);
sensorManager=(SensorManager)getSystemService(SENSOR_SERVICE);
List<Sensor> sensors = sensorManager.getSensorList(Sensor.TYPE_ALL);
for (Sensor sensor : sensors) {
sensorsInMyPhone = sensorsInMyPhone + sensor.getName() + "\n";
}
sensorList.setText(sensorsInMyPhone);
}
@Override
protected void onDestroy() {
super.onDestroy();
}
}