Use of andrid Sensor

Source: Internet
Author: User

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 ();}}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.