Android实现传感器功能

  import android.hardware.Sensor;

  import android.hardware.SensorEvent;

  import android.hardware.SensorEventListener;

  import android.hardware.SensorManager;

  import android.support.v7.app.AppCompatActivity;

  import android.os.Bundle;

  import android.util.Log;

  import android.widget.TextView;

  public class MainActivity extends AppCompatActivity implements SensorEventListener {

  private TextView tvAccelerometer;

  private SensorManager sensorManager;

  private float[] gravity = new float[3];

  @Override

  protected void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);

  setContentView(R.layout.activity_main);

  tvAccelerometer = (TextView) findViewById(R.id.tvAccelerometer);

  sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);

  }

  @Override

  public void onSensorChanged(SensorEvent event) {

  switch (event.sensor.getType()){

  case Sensor.TYPE_ACCELEROMETER:

  final float alpha = (float)0.8;

  gravity[0] = alpha*gravity[0]+(1-alpha)*event.values[0];

  gravity[1] = alpha*gravity[1]+(1-alpha)*event.values[1];

  gravity[2] = alpha*gravity[2]+(1-alpha)*event.values[2];

  String accelerometer = "加速度

  "+"X:"+(event.values[0]-gravity[0])+"

  "+"Y:"+

  (event.values[1]-gravity[1])+"

  "+"Z:"+(event.values[2]-gravity[2]);

  Log.d("Z",String.valueOf((event.values[2]-gravity[2])));

  tvAccelerometer.setText(accelerometer);

  break;

  case Sensor.TYPE_GRAVITY:

  gravity[0] = event.values[0];

  gravity[1] = event.values[1];

  gravity[2] = event.values[2];

  break;

  case Sensor.TYPE_PROXIMITY:

  setTitle(String.valueOf(event.values[0]));

  default:

  break;

  }

  }

  @Override

  public void onAccuracyChanged(Sensor sensor, int accuracy) {

  }

  protected void onResume(){

  super.onResume();

  sensorManager.registerListener(this,sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),

  SensorManager.SENSOR_DELAY_UI);

  sensorManager.registerListener(this,sensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY),

  SensorManager.SENSOR_DELAY_FASTEST);

  sensorManager.registerListener(this,sensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY),

  SensorManager.SENSOR_DELAY_NORMAL);

  }

  protected void onPause(){

  super.onPause();

  sensorManager.unregisterListener(this);

  }

  }