Package com. android. ctbri. SensorTest;
Import java. util. Random;
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;
Import android. widget. Toast;
Public class SensorTest extends Activity {
Private SensorManager sensorMgr;
Private float x, y, z;
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
Final TextView mTextView = (TextView) findViewById (R. id. textview1 );
// Start motion detection
SensorMgr = (SensorManager) getSystemService (SENSOR_SERVICE );
SensorEventListener lsn = new SensorEventListener (){
Public void onSensorChanged (SensorEvent e ){
X = e. values [SensorManager. DATA_X];
Y = e. values [SensorManager. DATA_Y];
Z = e. values [SensorManager. DATA_Z];
// Angle = Math. atan (x/y );
// If (angle-last_angle)> 0.5)
// | (Angle-last_angle) <-0.5 )){
// Toast. makeText (SensorTest. this, "Love", 1000). show ();
Random mRandom = new Random ();
Int Red = mRandom. nextInt (30 );
String str = "" + Red;
If (x> 7 & y> 6 & z <8 ){
Toast. makeText (SensorTest. this, str, 1000). show ();
}
MTextView. setText ("x =" + (int) x + "," + "y =" + (int) y + ","
+ "Z =" + (int) z );
//} Else {
// Toast. makeText (SensorTest. this, "I gao ni", 1000). show ();
//}
}
Public void onAccuracyChanged (Sensor s, int accuracy ){
// ToDo
}
};
// SensorMgr. SENSOR_DELAY_GAME
Sensor sensor = sensorMgr. getdefadefasensor (Sensor. TYPE_ACCELEROMETER); //. SENSOR_ACCELEROMETER );
SensorMgr
. RegisterListener (lsn, sensor, SensorManager. SENSOR_DELAY_GAME/* SENSOR_DELAY_GAME */);
}
@ Override
Public void onBackPressed (){
// TODO Auto-generated method stub
Super. onBackPressed ();
Android. OS. Process. killProcess (android. OS. Process. myPid ());
}
}
From the column of gongzibai