Android shake detection shakedetector

Source: Internet
Author: User

Recently, I made a program to implement the features of a shake mobile phone.

Think of this function may be widely used, such as shaking mobile phones for pictures, shuffling, and ending the current program, so I found some information and made improvements, this function is encapsulated into a class (shakedetector) for future use.

Http://blog.csdn.net/ZhengZhiRen/archive/2010/10/09/5930451.aspx

Shake detection is based on the accelerator sensor (sensor. type_accelerometer ).

Due to the presence of gravity, acceleration sensors also have acceleration when mobile phones are still on the desktop.

Therefore, it is impossible to determine the shaking by checking whether there is an acceleration.

So, judge the acceleration change...

Calculate the difference value of acceleration at a short interval. Compare it with a specified threshold. If the difference value is greater than the threshold value, it is assumed that the shaking occurred.

The clingmarks method simply adds the acceleration differences in the X, Y, and Z directions. I don't think it is very accurate.

Acceleration is a vector, and the difference must be the square of the difference in each direction before addition. (I forgot my mathematics. Remember it wrong ...)

So we have this line of code.

 

Float Delta = floatmath. SQRT (deltax * deltax + deltay * deltay + deltaz * deltaz)/difftime * 10000; 

Function encapsulated into a classShakedetectorThe sensoreventlistener interface is implemented to register the listener of sensor events with the system.

 

Package zhengzhiren. android. hardware; <br/> Import Java. util. arraylist; <br/> Import android. content. context; <br/> Import android. hardware. sensor; <br/> Import android. hardware. sensorevent; <br/> Import android. hardware. sensoreventlistener; <br/> Import android. hardware. sensormanager; <br/> Import android. util. floatmath; <br/>/** <br/> * used to detect mobile phone shake <br/> * @ author Zheng zhiren <br/> * @ See <a href = "http: // B Log.csdn.net/zhengzhiren "mce_href =" http://blog.csdn.net/zhengzhiren "> blog </a> <br/> */<br/> public class shakedetector implements sensoreventlistener {<br/>/** <br/> * interval of detection <br/> */<br/> static final int update_interval = 100; <br/>/** <br/> * time of the last detection <br/> */<br/> long mlastupdatetime; <br/>/** <br/> * the component of the acceleration in the direction of X, Y, and Z during the last detection, which is used to compare with the current acceleration. <Br/> */<br/> float mlastx, mlasty, mlati; <br/> context mcontext; <br/> sensormanager msensormanager; <br/> arraylist <onshakelistener> mlisteners; <br/>/** <br/> * The shaking detection threshold determines the degree of sensitivity to shaking. <Br/> */<br/> Public int shakethreshold = 5000; <br/> Public shakedetector (context) {<br/> mcontext = context; <br/> msensormanager = (sensormanager) Context <br/>. getsystemservice (context. sensor_service); <br/> mlisteners = new arraylist <onshakelistener> (); <br/>}< br/>/** <br/> * When a shaking event occurs, receive notification <br/> */<br/> Public interface onshakelistener {<br/>/** <br/> * called when mobile phone shakes <br/> */ <br/> void ons Hake (); <br/>}< br/>/** <br/> * Register onshakelistener, receive notification when shaking <br/> * @ Param listener <br/> */<br/> Public void registeronshakelistener (onshakelistener listener) {<br/> If (mlisteners. contains (listener) <br/> return; <br/> mlisteners. add (listener ); <br/>}< br/>/** <br/> * remove the registered onshakelistener <br/> * @ Param listener <br/> * /<br/> Public void unregisteronshakelistener (onshakelistener Listener) {<br/> mlisteners. remove (listener); <br/>}< br/>/** <br/> * start shaking detection <br/> */<br/> Public void start () {<br/> If (msensormanager = NULL) {<br/> throw new unsupportedoperationexception (); <br/>}< br/> Sensor sensor = msensormanager <br/>. getdefasensensor (sensor. type_accelerometer); <br/> If (sensor = NULL) {<br/> throw new unsupportedoperationexception (); <br/>}< br/> Boolean success = msens Ormanager. registerlistener (this, sensor, <br/> sensormanager. sensor_delay_game); <br/> If (! Success) {<br/> throw new unsupportedoperationexception (); <br/>}< br/>/** <br/> * Stop shaking detection <br/> */<br/> Public void stop () {<br/> If (msensormanager! = NULL) <br/> msensormanager. unregisterlistener (this); <br/>}< br/> @ override <br/> Public void onaccuracychanged (sensor, int accuracy) {<br/> // todo auto-generated method stub <br/>}< br/> @ override <br/> Public void onsensorchanged (sensorevent event) {<br/> long currenttime = system. currenttimemillis (); <br/> long difftime = currenttime-mlastupdatetime; <br/> If (difftime <update_interval) <br/> return; <br/> mlastupdatetime = currenttime; <br/> float x = event. values [0]; <br/> float y = event. values [1]; <br/> float z = event. values [2]; <br/> float deltax = x-mlastx; <br/> float deltay = Y-mlasty; <br/> float deltaz = z-mlati; <br/> mlastx = x; <br/> mlasty = y; <br/> mlati = z; <br/> float Delta = floatmath. SQRT (deltax * deltax + deltay * deltay + deltaz <br/> * deltaz) <br/>/difftime * 10000; <br/> If (delta> shakethreshold) {// when the difference value of acceleration is greater than the specified threshold value, it is considered as a shake <br/> This. policylisteners (); <br/>}< br/>/** <br/> * When a shaking event occurs, notify all listener <br/> */<br/> private void policylisteners () {<br/> for (onshakelistener listener: mlisteners) {<br/> listener. onshake (); <br/>}< br/> 

How to Use shakedetector

  1. New shakedetector
  2. Call mshakedetector. registeronshakelistener () to register an onshakelistener.
  3. In the onshakelistener function, handle the shaking event.
  4. Call mshakedetector. Start () to start the shaking detection.
  5. Mshakedetector. Stop () is used to stop shaking Detection

References:

Http://www.clingmarks.com /? P = 25

Http://android.hlidskialf.com/blog/code/android-shake-detection-listener

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.