Android_mars Learning note _s05_006_ Distance sensor

Source: Internet
Author: User

1 Importandroid.app.Activity;2 ImportAndroid.content.Context;3 ImportAndroid.hardware.Sensor;4 Importandroid.hardware.SensorEvent;5 ImportAndroid.hardware.SensorEventListener;6 ImportAndroid.hardware.SensorManager;7 ImportAndroid.os.Bundle;8 9  Public classMainactivityextendsActivityImplementsSensoreventlistener {Ten  One      PrivateSensormanager Msensormanager; A       PrivateSensor mproximity; -  - @Override the        Public Final voidonCreate (Bundle savedinstancestate) { -         Super. OnCreate (savedinstancestate); - Setcontentview (r.layout.main); -  +         //get an instance of the sensor service, and use this to Get a instance of -         //a particular sensor. +Msensormanager =(Sensormanager) Getsystemservice (context.sensor_service); AMproximity =msensormanager.getdefaultsensor (sensor.type_proximity); at       } -  - @Override -        Public Final voidOnaccuracychanged (sensor sensor,intaccuracy) { -         //Do something the here if sensor accuracy changes. -       } in  - @Override to        Public Final voidonsensorchanged (Sensorevent event) { +         floatDistance = Event.values[0]; -SYSTEM.OUT.PRINTLN ("Distance--->" +distance); the System.out.println (Mproximity.getmaximumrange ()); *         //Do something with this sensor data. $       }Panax Notoginseng  - @Override the       protected voidOnresume () { +         //Register a listener for the sensor. A         Super. Onresume (); theMsensormanager.registerlistener ( This, mproximity, sensormanager.sensor_delay_normal); +       } -  $ @Override $       protected voidOnPause () { -         //Being sure to unregister the sensor when the activity pauses. -         Super. OnPause (); theMsensormanager.unregisterlistener ( This); -       }Wuyi  the      -}

Android_mars Learning note _s05_006_ Distance sensor

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.