<uses-permission android:name= "Android.permission.VIBRATE"/>
Package Com.buzz.shake;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;/** * "Shake" * @author Nickchung */public class Sensoractivity extends Activi ty {private Sensormanager sensormanager;private vibrator vibrator;private static final String TAG = "Sensoractivity";p Riv ATE static final int sensor_shake = 10;/** Called when the activity is first created. */@Overridepublic void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.main); Sensormanager = (Sensormanager) getsystemservice (sensor_service); vibrator = (Vibrator) Getsystemservice (Vibrator_service);} @Overrideprotected void Onresume () {super.onresume (); if (Sensormanager! = null) {//Register listener Sensormanager.registerliStener (Sensoreventlistener, Sensormanager.getdefaultsensor (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}} @Overrideprotected void OnPause () {super.onpause () ; if (Sensormanager! = null) {//Cancels listener Sensormanager.unregisterlistener (Sensoreventlistener);}} /** * Gravity Sensing monitor */private sensoreventlistener Sensoreventlistener = new Sensoreventlistener () {@Overridepublic void onsensor Changed (Sensorevent event) {//The sensor information is changed when the method is executed float[] values = event.values;float x = values[0];//x-axis direction of gravity, 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 positive log.i (TAG, "x-axis of gravity" + x + ", y-axis direction of the gravitational acceleration" + y + "; z-axis direction of the acceleration of gravity" + Z);//generally in these three directions the acceleration of gravity reached 40 to shake the phone state. int mediumvalue = 19;if (Math.Abs (x) > Mediumvalue | | Math.Abs (y) > Mediumvalue | | Math.Abs (z) > Mediumvalue) {vibrator.vibrate (200); Message msg = new Message (); msg.what = Sensor_shake;handler.sendmessage (msg);}} @Overridepublic void onaccuracychanged (Sensor sensor, int accuracy) {}};/** * action execution */handler Handler = new Handler () {@Overridepublic void Handlemessage (Message msg) {sup Er.handlemessage (msg); switch (msg.what) {case SENSOR_SHAKE:Toast.makeText (testsensoractivity.this), "Shake is detected, perform operation!" ", Toast.length_short). Show (); LOG.I (TAG, "shake detected, perform operation!") "); break;}};}
Ref: Shake-shake function, Android gravity sensor development
Android Sensor Shake (WeChat)