请先参考
浅入浅出Android(006):实时获取加速计Accelerometer的信息。
不多说,先上代码再配图。
1、建立android项目
2、修改布局layout/main.xml
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="有哪些传感器?"
/>
android:id="@+id/sensor_list"
android:text=""
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
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 sensors = sensorManager.getSensorList(Sensor.TYPE_ALL);
for (Sensor sensor : sensors) {
sensorsInMyPhone = sensorsInMyPhone + sensor.getName() + "\n";
}
sensorList.setText(sensorsInMyPhone);
}
@Override
protected void onDestroy() {
super.onDestroy();
}
}
4、效果图