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
[Java]
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 monitoring-"Shake"
*
* @ Author single Hongyu
*
*/
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 );
SensorManager = (SensorManager) getSystemService (SENSOR_SERVICE );
Vibrator = (Vibrator) getSystemService (VIBRATOR_SERVICE );
}
@ Override
Protected void onResume (){
Super. onResume ();
If (sensorManager! = 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 onStop (){
Super. onStop ();
If (sensorManager! = Null) {// cancel the listener
SensorManager. unregisterListener (sensorEventListener );
}
}
/**
* Gravity Sensing Monitoring
*/
Private SensorEventListener sensorEventListener = new SensorEventListener (){
@ Override
Public void onSensorChanged (SensorEvent event ){
// Execute this method when the sensor information changes
Float [] values = event. values;
Float x = values [0]; // gravity acceleration in the x axis direction, positive to the right
Float y = values [1]; // acceleration of gravity in the y axis, positive forward
Float z = values [2]; // acceleration of gravity in the z axis, positive upward
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 handleMessage (Message msg ){
Super. handleMessage (msg );
Switch (msg. what ){
Case SENSOR_SHAKE:
Toast. makeText (testsens?ti=. this, "Shake detected and executed! ", Toast. LENGTH_SHORT). show ();
Log. I (TAG, "Shake detected. execute the operation! ");
Break;
}
}
};
}