1. The changes of gravity acceleration on the three axes are displayed;
2. click the button to play or stop the music.
Package COM. CJF. sersor; import android. app. activity; import android. hardware. sensor; import android. hardware. sensorevent; import android. hardware. sensoreventlistener; import android. hardware. sensormanager; import android. media. mediaplayer; import android. OS. bundle; import android. view. view; import android. view. view. onclicklistener; import android. widget. button; import android. widget. textview; public class sens Extends Tianyi extends activity implements sensoreventlistener {private sensormanager; private sensor defaultsensor; private textview x, y, z; Private Static mediaplayer; private button; @ override public void oncreate (bundle release) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); X = (textview) This. findviewbyid (R. id. x); y = (textview) This. f Indviewbyid (R. id. y); Z = (textview) This. findviewbyid (R. id. z); button = (button) This. findviewbyid (R. id. button); sensormanager = (sensormanager) This. getsystemservice (sensor_service); defaultsensor = sensormanager. getdefasensensor (sensormanager. sensor_accelerometer); sensormanager. registerlistener (this, defaultsensor, sensormanager. sensor_delay_fastest); mediaplayer = mediaplayer. create (this, R. RA W. music); // create a mediaplayer instance button. setonclicklistener (New onclicklistener () {// register the playing music event public void onclick (view v) {play ();}});} // This method is continuously called public void onsensorchanged (sensorevent event) {X. settext ("x =:" + event. values [0]); Y. settext ("Y =:" + event. values [1]); Z. settext ("z =:" + event. values [2]);} // This method is automatically called when the accuracy rate of the registered sensor changes. Public void onaccuracychanged (sensor, int accuracy) {// todo auto- Generated method stub} @ override // unregister protected void ondestroy () {// release all sensor listening sensormanager. unregisterlistener (this); super. ondestroy () ;}// play or stop the music private void play () {If (! Mediaplayer. isplaying () {mediaplayer. Start () ;}else {mediaplayer. Stop (); mediaplayer. Release ();}}}