Android acceleration sensor enables "Shake" with mobile phone Vibration

Source: Internet
Author: User

Because there are a lot of codes, I wrote them separately, and the comments are still detailed to facilitate the study of 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 the direction of the drawer listener switch button // get the Vibrator service mVibrator = (Vibrator) getApplication (). getSystemService (VIBRATOR_SERVICE); // instantiate the acceleration sensor detection class mShakeListener = new ShakeListener (ShakeActivity. this); mShakeListener. setOnShakeListener (new OnShakeListener () {public Void onShake () {mShakeListener. stop (); startVibrato (); // start to vibrate new Handler (). postDelayed (new Runnable () {@ Overridepublic void run () {Toast mtoast; mtoast = Toast. makeText (ShakeActivity. this, "Haha, success !. \ N try again! ", 1000); mtoast. show (); mVibrator. cancel (); mShakeListener. start () ;}}, 2000) ;}}) ;}// defines the public void startVibrato () {mVibrator. vibrate (new long [] {500,200,500,200},-1); // The first {} contains the rhythm array, // The second parameter is the number of repetitions, and-1 is not repeated, if the value is not-1, it starts to repeat the specified subscript of pattern.} @ Overrideprotected void onDestroy () {super. onDestroy (); if (mShakeListener! = Null) {mShakeListener. stop () ;}} ShakeListener/*** a listener * accelerometer * values [0]: x-axis acceleration * values [1]: y-axis direction acceleration * values [2]: z-axis direction acceleration */public class ShakeListener implements SensorEventListener {// The speed threshold, when the shaking speed reaches this value, private static final int SPEED_SHRESHOLD = 3000; // interval between the two checks: private static final int UPTATE_INTERVAL_TIME = 70; // sensor manager private SensorManager sensorManager; // Sensor Private Sensor sensor; // gravity Sensor listener private OnShakeListener onShakeListener; // Context private Context mContext; // gravity sensing coordinate private float lastX; private float lastY when the last position on the mobile phone; private float lastZ; // last detection time private long lastUpdateTime; // The constructor public ShakeListener (Context c) {// obtain the listening object mContext = c; start ();} // start public void start () {// obtain the sensor manager sensorManager = (SensorManager) mContext. getSystemService (Context. SENSOR_S ERVICE); if (sensorManager! = Null) {// obtain the gravity sensor Sensor = sensorManager. getdefasensensor (sensor. TYPE_ACCELEROMETER);} // register if (sensor! = Null) {// There are also SENSOR_DELAY_UI, SENSOR_DELAY_FASTEST, SENSOR_DELAY_GAME, etc. // depending on the application, the desired response rate is different. Set sensorManager according to the actual situation. registerListener (this, sensor, SensorManager. SENSOR_DELAY_NORMAL) ;}}// stop public void stop () {sensorManager. unregisterListener (this);} // set the gravity sensor listener public void setOnShakeListener (OnShakeListener listener) {onShakeListener = listener;} // The gravity sensor senses the change data public void onSensorChanged (SensorEvent) {// The current detection time is long currentUpdateTime = System. currentTimeMillis (); // interval between two checks long timeInterval = currentUpdateTime-lastUpdateTime; // you can determine whether the detection interval has been reached if (timeInterval <UPTATE_INTERVAL_TIME) return; // The current time is changed to lastUpdateTime = currentUpdateTime; // obtain the x, y, and zcoordinate float x = event. values [0]; float y = event. values [1]; float z = event. values [2]; // obtain the changing values of x, y, and z, float deltaX = x-lastX; float deltaY = y-lastY; float deltaZ = z-lastZ; // convert the current coordinate to the last coordinate lastX = x; lastY = y; lati = z; double speed = Math. sqrt (deltaX * deltaX + deltaY * deltaY + deltaZ * deltaZ)/timeInterval * 10000; // if (speed> = SPEED_SHRESHOLD) {onShakeListener is prompted when the speed threshold is reached. onShake () ;}}// this method is called back when the Sensor precision changes. public void onAccuracyChanged (sensor Sensor, int accuracy) {}// shake listener interface public interface OnShakeListener {public void onShake ();}}

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.