在上一篇中,讲到了怎么获取手机的传感器http://blog.csdn.net/nainai007/article/details/7615364,在这里是获取传感器的值。
要获取传感器的值,首先要为应用程序注册传感器时间,然后接受传感器传来的数据。
MainActivity的代码为:
package com.nine.sensordemo;
import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends Activity {
private SensorManager sm;
private Sensor ligthSensor;
private StringBuffer sb;
private TextView tvValue;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
sb = new StringBuffer();
tvValue = (TextView) findViewById(R.id.tvVal