Shake the Andorid Sensor

Source: Internet
Author: User
Package com. llm; import java. util. list; import android. app. activity; import android. hardware. sensor; import android. hardware. sensorEvent; import android. hardware. sensorEventListener; import android. hardware. sensorManager; import android. OS. bundle; import android. widget. toast; public class testsens%ti%extends Activity {private static final int FORCE_THRESHOLD = 900; private long lastShakeTime; // priv Ate long presentShakeTime; private long duration; // time difference private float lastX = 0.0f; private float lastY = 0.0f; private float lati = 0.0f; private float presentX = 0.0f; private float presentY = 0.0f; private float presentZ = 0.0f; private float currenForce; // The shaking speed @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); Senso RManager sm = (SensorManager) getSystemService (SENSOR_SERVICE); List <Sensor> ls = sm. getSensorList (Sensor. TYPE_ACCELEROMETER); if (ls. size ()> 0) {Sensor s = ls. get (0); sm. registerListener (new SensorEventListener () {public void onSensorChanged (SensorEvent event) {// return if (event. sensor. getType ()! = Sensor. TYPE_ACCELEROMETER | event. values. length <3) return; presentX = event. values [SensorManager. DATA_X]; presentY = event. values [SensorManager. DATA_Y]; presentZ = event. values [SensorManager. DATA_Z]; presentShakeTime = System. currentTimeMillis (); if (presentShakeTime-lastShakeTime> 100) {duration = (presentShakeTime-lastShakeTime); lastShakeTime = presentShakeTime; currenForce = Math. abs (presentX + presentY + presentZ-lastX-lastY-lati)/duration * 10000; if (currenForce> FORCE_THRESHOLD) {// The shaking rate is greater than 900 Toast. makeText (testsens?ti=. this, "Shake success", 1000 ). show () ;}lastx = presentX; lastY = presentY; lati = presentZ ;}}; public void onAccuracyChanged (Sensor sensor, int accuracy) {}, s, SensorManager. SENSOR_DELAY_NORMAL );}}}

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.