Android accelerometer for "Shake", with mobile phone vibration

Source: Internet
Author: User

Because the code is a bit more, so separate write, comments are detailed, easy to learn activitypackage Com.lmw.android.test; import Android.app.activity;import Android.os.bundle;import Android.os.handler;import Android.os.vibrator;import Android.view.View;import Android.widget.toast; import Com.lmw.android.test.shakelistener.onshakelistener; public Class Shakeactivity extends Activity { shakelistener mshakelistener = null; Vibrator mvibrator;  @Overridepublic void OnCreate (Bundle savedinstancestate) {//TODO auto-generated method Stubsuper.oncreate (savedinstancestate); Setcontentview (r.layout.shake);//Drawerset ();//Set Drawer Listen toggle button Direction  / /Get Vibrator Service Mvibrator = (vibrator) getapplication (). Getsystemservice (Vibrator_service);//Instantiate acceleration sensor detection class Mshakelistener = New Shakelistener (Shakeactivity.this);  mshakelistener.setonshakelistener (new Onshakelistener () { public void Onshake () {mshakelistener.stop (); Startvibrato ();//Start shaking New Handler (). postdelayed (New Runnable () {@ overridepublic void Run () {Toast mtoast;mtoast = ToaSt.maketext (Shakeactivity.this, "hehe, success!. \ n Try it again! Mtoast.show (); Mvibrator.cancel (); Mshakelistener.start ();}}, 2000);}); Define vibrate public void Startvibrato () {mvibrator.vibrate (new long[] {500, 200, 500, 200},-1);//The first {} Inside is the rhythm array,//The second parameter is the number of repetitions, -1 for non-repetition, non-1 to start repeating}  @Overrideprotected void OnDestroy () {Super.ondestroy () from the specified subscript of pattern, if (mshakelistener! = null ) {mshakelistener.stop ();}}}  shakelistener /** * a monitor for mobile phone shake  * accelerometer  * values[0]: X-axis direction acceleration  * values[1]: Y-axis direction Acceleration  * values[2]: Z-axis direction acceleration  */public class Shakelistener implements Sensoreventlistener {//Speed threshold, When the shaking speed reaches this value, the function of the private static final int speed_shreshold = 3000;//two times the interval of detection private static final int uptate_interval_time = 70;//Sensor Manager Private Sensormanager sensormanager;//sensor private sensor sensor;//gravity sense listener Private Onshakelistener onshakelistener;//Context Private Context mcontext;//phone last position when gravity sensing coordinates private float lastx;private float lasty;private float lastz;//Last detection Time private loNg lastupdatetime; //Constructor Public Shakelistener (Context c) {//Get Listener Object Mcontext = C;start ();}  //start public void start () {//Get sensor manager Sensormanager = (sensormanager) mcontext.getsystemservice (context.sensor_ SERVICE); if (Sensormanager! = null) {//get gravity sensor sensors = sensormanager.getdefaultsensor (sensor.type_accelerometer);} Register if (sensor! = NULL) { //also has sensor_delay_ui, sensor_delay_fastest, Sensor_delay_game, etc.,  //according to different applications, The response rate is different, depending on the actual situation set Sensormanager.registerlistener (this, sensor,sensormanager.sensor_delay_normal);}  } //stop detection public void Stop () {Sensormanager.unregisterlistener (this);}  //set Gravity sense listener public void Setonshakelistener (Onshakelistener listener) {Onshakelistener = listener;}  //Gravity sensor Sensing get change data public void onsensorchanged (Sensorevent event) {//Now detection time long currentupdatetime = System.currenttimemillis ();//Two detection interval long timeinterval = currentupdatetime-lastupdatetime;//determine if the detection interval is reached if ( TimeInterval < Uptate_interval_time) return;//now time becomes last time lastUpdateTime = currentupdatetime; //get x, y, z coordinates float × = event.values[0];float y = event.values[1];float z = event.value s[2]; //gets the change value of x, Y, z, float deltax = x-lastx;float DeltaY = y-lasty;float Deltaz = z-lastz; //Turn the current coordinates into the last seat Mark Lastx = X;lasty = Y;lastz = z; double Speed = math.sqrt (DeltaX * deltax + deltay * deltay + deltaz* Deltaz)/timeInt Erval * 10000; //to reach the speed threshold, issue the prompt if (speeds >= speed_shreshold) {onshakelistener.onshake ();}} Callback the method when the sensor accuracy changes public void onaccuracychanged (sensor sensor, int accuracy) { } //Shake the Listener interface public interface Onshakelistener {public void Onshake ();}  } 

Android accelerometer for "shake" with mobile phone vibration

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.