Android -- shake, android
To implement the "Shake" function, it is actually very simple. It is to detect the mobile phone's gravity sensor. The specific implementation code is as follows:
1. Add operation permissions in AndroidManifest. xml
<Uses-permission android: name = "android. permission. VIBRATE"/>
II. Implementation 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;/*** Android mobile phone shake listener -- "Shake" ** @ author single red Yu **/public class testsens?ti=extends Activity {private SensorManager sensorManager; private Vibrator vibrator; private static final String TAG = "testsens?ti="; private static final int SENSOR_SHAKE = 10; /** Called when the activity is first created. * // @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); s EnsorManager = (SensorManager) getSystemService (SENSOR_SERVICE); vibrator = (Vibrator) getSystemService (VIBRATOR_SERVICE) ;}@ Override protected void onResume () {super. onResume (); if! = Null) {// register the listener sensorManager. registerListener (sensorEventListener, sensorManager. getdefasensensor (Sensor. TYPE_ACCELEROMETER), SensorManager. SENSOR_DELAY_NORMAL); // The first parameter is Listener, the second parameter is the sensor type, and the third parameter value obtains the frequency of sensor information.} @ Override protected void onPause () {super. onPause (); if (sensorManager! = Null) {// cancel the listener sensorManager. unregisterListener (sensorEventListener);}/*** gravity sensing listener */private SensorEventListener sensorEventListener = new SensorEventListener () {@ Override public void onSensorChanged (SensorEvent event) {// execute this method float [] values = event when the sensor information is changed. values; float x = values [0]; // acceleration of gravity in the x axis, float y = values [1] to the right; // acceleration of gravity in the y axis, forward is float z = values [2]; // acceleration of gravity in the z axis direction, upward is positive Log. I (TAG, "acceleration of gravity in the x axis" + x + "; acceleration of gravity in the y axis" + y + "; acceleration of gravity in the z axis" + z ); // generally, when the acceleration of gravity in these three directions reaches 40, the mobile phone is shaken. Int medumValue = 19; // Samsung i9250 cannot shake more than 20. No way, set only 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 handle Message (Message msg) {super. handleMessage (msg); switch (msg. what) {case SENSOR_SHAKE: Toast. makeText (testsens?ti=. this, "the shaking is detected and the operation is executed! ", Toast. LENGTH_SHORT). show (); Log. I (TAG," Shake detected, perform the operation! "); Break ;}}};}