Using Sensor for Android phones is similar to WeChat shake

Source: Internet
Author: User

There are several types of sensors:
Direction sensor: sensor. type_orientation
Acceleration (gravity) sensor: sensor. type_accelerometer
Light Sensor: sensor. type_light
Magnetic Field Sensor: sensor. type_magnetic_field
Distance (proximity) sensor: sensor. type_proximity
Temperature Sensor: sensor. type_temperature

 

Here we use the acceleration sensor.

 

 

Package COM. jamin. sensor; import android. app. activity; import android. app. service; import android. hardware. sensor; import android. hardware. sensorevent; import android. hardware. sensoreventlistener; import android. hardware. sensormanager; import android. OS. bundle; import android. OS. vibrator; import android. util. log; import android. view. view; import android. widget. button; import android. widget. textview; Public clas S sensortestactivity extends activity implements sensoreventlistener {/** called when the activity is first created. */textview TV = NULL; button = NULL; sensormanager = NULL; vibrator = NULL; @ overridepublic void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); button = (button) findviewbyid (R. id. button1); TV = (textview) Findviewbyid (R. id. textview1); button. setonclicklistener (new view. onclicklistener () {@ overridepublic void onclick (view v) {// todo auto-generated method stubtv. settext ("text restoration") ;}}); sensormanager = (sensormanager) getsystemservice (sensor_service); vibrator = (vibrator) getsystemservice (service. vibrator_service) ;}@ overrideprotected void onpause () {// todo auto-generated method stubsuper. onpause (); sens Ormanager. unregisterlistener (this) ;}@ overrideprotected void onresume () {// todo auto-generated method stubsuper. onresume (); sensormanager. registerlistener (this, sensormanager. getdefasensensor (sensor. type_accelerometer), sensormanager. sensor_delay_normal);} @ overridepublic void onaccuracychanged (sensor, int accuracy) {// call back this method when the sensor precision changes, do nothing .} @ overridepublic void onsensorchanged (sensoreven T event) {int sensortype = event. sensor. getType (); // values [0]: X axis, values [1]: Y axis, values [2]: Z axis float [] values = event. values; If (sensortype = sensor. type_accelerometer) {If (math. ABS (Values [0])> 17 | math. ABS (Values [1])> 17 | math. ABS (Values [2])> 17) {log. D ("sensor", "============= values [0] =" + values [0]); log. D ("sensor", "============= values [1] =" + values [1]); log. D ("sensor", "============= values [2] = "+ Values [2]); TV. settext (" Shake "); // After shaking the mobile phone, it will be accompanied by a vibration prompt ~~ Vibrator. vibrate (500 );}}}}

Remember to switch the sensor listening event during onresume and onpause.

If you want to adjust the sensitivity of the sensor, modify it. The value [0, 1, 2] is greater than the upper limit.

 

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.