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;/** * Android Shaking phone monitoring--"shake" * * @author Tan Hongyu * */public class Testsensoractivity extends Activity {private Sensormanager sensormanager;private Vibrator vibrator;private static final String TAG = "testsensoractivity";p rivate STA Tic 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 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);}} @OverRidepublic void onaccuracychanged (sensor sensor, int accuracy) {}};/** * action execution */handler Handler = new Handler () {@Override public void Handlemessage (Message msg) {super.handlemessage (msg), switch (msg.what) {case SENSOR_SHAKE:Toast.makeText ( Testsensoractivity.this, "The shaking is detected and the operation is performed!" ", Toast.length_short). Show (); LOG.I (TAG, "shake detected, perform operation!") "); break;}};}
Android Interview--Shake It