Android sensor usage

Source: Internet
Author: User

Public class sensortest extends activity implements sensoreventlistener {sensormanager = NULL; // For each values textview outputx; textview outputy; textview outputz; // for orientation values textview outputx2; textview outputy2; textview outputz2; @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); // initialize sensormanager = (sensormanager) getsystemservice (sensor_service); setcontentview (R. layout. main); // just some textviews, for data output outputx = (textview) findviewbyid (R. id. textview01); outputy = (textview) findviewbyid (R. id. textview02); outputz = (textview) findviewbyid (R. id. textview03); outputx2 = (textview) findviewbyid (R. id. textview04); outputy2 = (textview) findviewbyid (R. id. textview05); outputz2 = (textview) findviewbyid (R. id. textview06);} // register the listener @ override protected void onresume () {super. onresume (); sensormanager. registerlistener (this, sensormanager. getdefasensensor (sensor. type_accelerometer), sensormanager. sensor_delay_game); sensormanager. registerlistener (this, sensormanager. getdefasensensor (sensor. type_orientation), sensormanager. sensor_delay_game);} // log out of listening in onstop @ override protected void onstop () {super. onstop (); sensormanager. unregisterlistener (this, sensormanager. getdefasensensor (sensor. type_accelerometer); sensormanager. unregisterlistener (this, sensormanager. getdefasensensor (sensor. type_orientation);} // public void onsensorchanged (sensorevent event) {synchronized (this) {Switch (event. sensor. getType () {Case sensor. type_accelerometer: outputx. settext ("X:" + float. tostring (event. values [0]); outputy. settext ("Y:" + float. tostring (event. values [1]); outputz. settext ("Z:" + float. tostring (event. values [2]); break; Case sensor. type_orientation: outputx2.settext ("X:" + float. tostring (event. values [0]); outputy2.settext ("Y:" + float. tostring (event. values [1]); outputz2.settext ("Z:" + float. tostring (event. values [2]); break ;}}// more precise change @ override public void onaccuracychanged (sensor, int accuracy ){}}

To sum things up, to access sensor data you have to do the following things:
1. Check sensor availability.
2. Register a listener to a sensormanager.
3. Catch the needed data, from onsensorchanged.
4. unregister the sensormanager's listener.

Other references

1, http://www.ibm.com/developerworks/opensource/library/os-android-sensor/index.html? CA = dgr-lnxw09Android-Sensors & s_tact = 105agx59 & s_cmp = grlnxw09

Related Article

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.