package com.cn;

import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.EditText;

public class Main extends Activity implements SensorEventListener {
	// ¶¨ÒåÏµÍ³µÄSensor¹ÜÀíÆ÷
		SensorManager sensorManager;
		EditText etTxt1;
        
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    	// »ñÈ¡³ÌÐò½çÃæÉÏµÄÎÄ±¾¿ò×é¼þ
		etTxt1 = (EditText) findViewById(R.id.txt1);
		// »ñÈ¡ÏµÍ³µÄ´«¸ÐÆ÷¹ÜÀí·þÎñ
		sensorManager = (SensorManager) getSystemService(
			Context.SENSOR_SERVICE);
    }
    public void onAccuracyChanged(Sensor sensor, int accuracy)
	{
	}
	protected void onResume()
	{
		super.onResume();
		// ÎªÏµÍ³µÄ¼ÓËÙ¶È´«¸ÐÆ÷×¢²á¼àÌýÆ÷
	 
		sensorManager.registerListener(this,
			sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
			SensorManager.SENSOR_DELAY_GAME);
	}
   
	protected void onStop()
	{
		// È¡Ïû×¢²á
		sensorManager.unregisterListener(this);
		super.onStop();
	}
 // ÒÔÏÂÊÇÊµÏÖSensorEventListener½Ó¿Ú±ØÐëÊµÏÖµÄ·½·¨
 	// µ±´«¸ÐÆ÷µÄÖµ·¢Éú¸Ä±äÊ±»Øµ÷¸Ã·½·¨
  
 	public void onSensorChanged(SensorEvent event)
 	{
 		float[] values = event.values;
 		StringBuilder sb = new StringBuilder();
 		sb.append("X·½ÏòÉÏµÄ¼ÓËÙ¶È£º");
 		sb.append((int)values[0]);
 		sb.append("\nY·½ÏòÉÏµÄ¼ÓËÙ¶È£º");
 		sb.append(values[1]);
 		sb.append("\nZ·½ÏòÉÏµÄ¼ÓËÙ¶È£º");
 		sb.append(values[2]);
 		etTxt1.setText(sb.toString());
 	}

}