package mutou.test; 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.util.Log; import android.widget.TextView; public class Sample46Activity extends Activity { TextView tvx; TextView tvy; TextView tvz; SensorManager mySensorManager; Sensor acSensor; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); tvx = (TextView)this.findViewById(R.id.tvx); tvy = (TextView)this.findViewById(R.id.tvy); tvz = (TextView)this.findViewById(R.id.tvz); mySensorManager = (SensorManager)getSystemService(SENSOR_SERVICE); acSensor = mySensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); } private SensorEventListener mySensorListener = new SensorEventListener(){ public void onAccuracyChanged(Sensor sensor, int accuracy) { } public void onSensorChanged(SensorEvent event) { if(event.sensor.getType() == 1) { float[] values = event.values; tvx.setText("x:" + values[0]); tvy.setText("y:" + values[1]); tvz.setText("z:" + values[2]); } } }; @Override public void onResume() { mySensorManager.registerListener( mySensorListener, acSensor, SensorManager.SENSOR_DELAY_UI); super.onResume(); } @Override public void onPause() { mySensorManager.unregisterListener(mySensorListener); super.onPause(); } }