</pre><pre name= "code" class= "java" ><span style= "font-family:arial; Background-color:rgb (255, 255, 255); " ><span style= "FONT-SIZE:18PX;" > recently made a program to implement the features of a shake phone. </span></span>
Think of this function may be widely used, such as shaking the phone for pictures, shuffle, the end of the current program, etc., so found some information, and improved, the function is packaged into a class (Shakedetector), convenient for future use.
Http://blog.csdn.net/ZhengZhiRen/archive/2010/10/09/5930451.aspx
The shake detection is based on the acceleration sensor (sensor.type_accelerometer).
Due to the presence of gravity, the acceleration sensor also has speed when the phone is stationary on the desktop.
Therefore, it is not possible to judge the shaking by only adding speed.
So, judging the change in acceleration ...
The difference in acceleration is calculated at a shorter time interval, compared to a specified threshold value, and if the difference is greater than the threshold, it is assumed that the wobble occurred.
The Clingmarks method adds the acceleration difference in the X, y, and z directions simply, which I think is not very accurate.
Acceleration is a vector, the difference should be the difference in the direction of the square after the sum, and then the root. (Math forgot, remember wrong ...) )
So there's this line of code
<span style= "FONT-SIZE:18PX;" >package Com.example.mywifi_list;import Java.util.arraylist;import Android.content.context;import Android.hardware.sensor;import Android.hardware.sensorevent;import Android.hardware.sensoreventlistener;import Android.hardware.sensormanager;import Android.util.floatmath;public class Shakedetector implements Sensoreventlistener {/** * detected interval */static final int update_interval = 100;/** * Last detected time */long mlastupdatetime;/** * Previous , the component of acceleration in x, Y, z direction is used to compare with the current speed difference. */float Mlastx, Mlasty, Mlastz; Context Mcontext; Sensormanager Msensormanager; arraylist<onshakelistener> mlisteners;/** * Shaking detection thresholds determine the degree of sensitivity to shaking, the smaller the more sensitive. */public int shakethreshold = 4000;public Shakedetector (context context) {Mcontext = Context;msensormanager = (SensorMana GER) Context.getsystemservice (context.sensor_service); mlisteners = new arraylist<onshakelistener> (); /** * When the shaking event occurs, receive notification */public interface Onshakelistener {/** * is called when the phone is shaken */void onshake ();} /** * Register Onshakelistener, when shakingNOTICE * * @param listener */public void Registeronshakelistener (Onshakelistener listener) {if (Mlisteners.contains (listene R)) Return;mlisteners.add (listener);} /** * Removal of registered Onshakelistener * * @param listener */public void Unregisteronshakelistener (Onshakelistener listener) {mlist Eners.remove (listener);} /** * Start shake detection */public void Start () {if (Msensormanager = = null) {throw new unsupportedoperationexception (); Sensor sensor = Msensormanager.getdefaultsensor (Sensor.type_accelerometer), if (sensor = = null) {throw new Unsupportedoperationexception ();} Boolean success = Msensormanager.registerlistener (this, sensor,sensormanager.sensor_delay_game); if (!success) {throw New Unsupportedoperationexception ();}} /** * Stop shaking detect */public void Stop () {if (Msensormanager! = null) Msensormanager.unregisterlistener (this);} @Overridepublic void onaccuracychanged (sensor sensor, int accuracy) {//TODO auto-generated method stub} @Overridepublic V OID onsensorchanged (Sensorevent event) {Long currenttime = System.curreNttimemillis (); Long difftime = Currenttime-mlastupdatetime;if (Difftime < update_interval) Return;mlastupdatetime = Currenttime;float x = event.values[0];float y = event.values[1];float z = event.values[2];float DeltaX = x-mlastx;float DeltaY = Y-mlasty;float Deltaz = Z-MLASTZ;MLASTX = X;mlasty = Y;mlastz = z;float Delta = floatmath.sqrt (DeltaX * Delta) X + deltay * deltay + deltaz* Deltaz)/difftime * 10000;IF (Delta > Shakethreshold) {//When the difference of acceleration is greater than the specified threshold, it is considered a shaking this.no Tifylisteners ();}} /** * When the shaking event occurs, notify all listener */private void Notifylisteners () {for (Onshakelistener listener:mlisteners) {Listener.onsh Ake ();}}} </span>
How to use Shakedetector
- New One Shakedetector
- Call Mshakedetector.registeronshakelistener () to register a Onshakelistener
- In the Onshake function of Onshakelistener, handle the Shaking event
- Call Mshakedetector.start () to start the shake detection
- Mshakedetector.stop () to stop shaking detection \
I write when the activity implements Onshakelistener, directly behind Setcontentview (R.layout.activity_main) Add
<span style= "FONT-SIZE:18PX;" >this.getapplicationcontext (); mshakedetector = new Shakedetector (this); Mshakedetector.registeronshakelistener ( this); Mshakedetector.start ();</span>
activity in the Onshake () method to write a shaking event
<span style= "FONT-SIZE:18PX;" > @Overridepublic void Onshake () {//TODO auto-generated method Stubtoast.maketext (Mainactivity.this, "Shake It! ", Toast.length_long). Show ();} </span>
stop detecting shaking, just write stop detection in OnStop
<span style= "FONT-SIZE:18PX;" >public void OnStop () {super.onstop (); Mshakedetector.stop ();} </span>
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Android Shake Detection Shakedetector