android--Shake a Shake

Source: Internet
Author: User

The realization of "shake a Shake" function, in fact, is very simple, is to detect the mobile phone's gravity sensor, the specific implementation code as follows:

First, add the Operation permission in the Androidmanifest.xml

<uses-permission android:name= "Android.permission.VIBRATE"/>

Second, the implementation of the Code

    Package com.xs.test;      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.os.Handler;      Import Android.os.Message;      Import Android.os.Vibrator;      Import Android.util.Log;            Import Android.widget.Toast;          Y {private Sensormanager Sensormanager;                Private vibrator Vibrator;          private static final String TAG = "testsensoractivity";                private static final int sensor_shake = 10; /** called when the activity is first created. */@Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstance              State); Setcontentview (R. Layout.main);              Sensormanager = (Sensormanager) getsystemservice (Sensor_service);          Vibrator = (Vibrator) getsystemservice (Vibrator_service);              } @Override protected void Onresume () {super.onresume (); if (Sensormanager! = null) {//Register listener Sensormanager.registerlistener (Sensoreventlistener, sensormanager.ge                  Tdefaultsensor (Sensor.type_accelerometer), sensormanager.sensor_delay_normal); The first parameter is listener, the second parameter is the resulting sensor type, the third parameter value gets the frequency of the Sensor information}} @Override protected Voi              D OnPause () {super.onpause ();              if (Sensormanager! = null) {//Cancels listener Sensormanager.unregisterlistener (Sensoreventlistener); }}/** * Gravity sensing monitor */private Sensoreventlistener Sensoreventlistener = New Sensoreventlistener () {@Override public VOID onsensorchanged (Sensorevent event) {//sensor information changed when executing this method float[] values = event.values; float x = values[0]; The gravitational acceleration in the x-axis direction, right is positive float y = values[1]; The gravitational acceleration of the y-axis, forward is positive float z = values[2];                  The gravitational acceleration of the z-axis, upward is the positive log.i (TAG, "The gravitational acceleration of the X-axis" + x + "; The gravitational acceleration of the y-axis" + y + "; z-axis direction of gravitational acceleration" + Z);                  Generally in these three directions the acceleration of gravity reached 40 to the state of shaking the phone. int medumvalue = 19;//Samsung i9250 No more than 20, no way, only set 19 if (Math.Abs (x) > Medumvalue | | Math.Abs (y) > Medumvalue | |                      Math.Abs (z) > Medumvalue) {vibrator.vibrate (200);                      Message msg = new Message ();                      Msg.what = Sensor_shake;                  Handler.sendmessage (msg);                    }} @Override public void onaccuracychanged (sensor sensor, int accuracy) {                }          };       /**   * Action Execution */Handler Handler = new Handler () {@Override public void Han                  Dlemessage (Message msg) {super.handlemessage (msg); Switch (msg.what) {case SENSOR_SHAKE:Toast.makeText (testsensoractivity.this, "shake detected Shake, perform the operation!                      ", Toast.length_short). Show (); LOG.I (TAG, "shake detected, perform operation!")                      ");                  Break      }              }                };   }


android--Shake a Shake

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.