Android implements the shake function, and android implements the shake function.
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]View plaincopy
- 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 onPause (){
- Super. onPause ();
- 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;
- }
- }
- };
- }
- This kind of shaking is common. Without the enhancement of the algorithm, the acceleration of gravity is 10.
- Algorithm Optimization:
- Package com. example. url;
Import java. io. IOException;
Import java.net. URL;
Import com. hahashijie. imageload. ImageLoader;
Import android. annotation. SuppressLint;
Import android. app. Activity;
Import android. graphics. Bitmap;
Import android. graphics. BitmapFactory;
Import android. graphics. drawable. Drawable;
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. ImageView;
@ SuppressLint ("HandlerLeak ")
Public class MainActivity extends Activity {
Private ImageView image;
Private SensorManager sensorManager;
Private Sensor sensor;
Private Vibrator vibrator;
Private static final int UPTATE_INTERVAL_TIME = 50;
Private static final int SPEED_SHRESHOLD = 30; // adjust the sensitivity of this value
Private long lastUpdateTime;
Private float lastX;
Private float lastY;
Private float lati;
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );
SensorManager = (SensorManager) getSystemService (SENSOR_SERVICE );
Vibrator = (Vibrator) getSystemService (VIBRATOR_SERVICE );
Image = (ImageView) findViewById (R. id. image );
}
@ Override
Protected void onResume (){
// TODO Auto-generated method stub
Super. onResume ();
If (sensorManager! = Null ){
Sensor = sensorManager. getdefasensensor (Sensor. TYPE_ACCELEROMETER );
}
If (sensor! = Null ){
SensorManager. registerListener (sensorEventListener,
Sensor,
SensorManager. SENSOR_DELAY_GAME); // select the sensor frequency here.
}
}
/**
* Gravity Sensing Monitoring
*/
Private SensorEventListener sensorEventListener = new SensorEventListener (){
@ Override
Public void onSensorChanged (SensorEvent event ){
Long currentUpdateTime = System. currentTimeMillis ();
Long timeInterval = currentUpdateTime-lastUpdateTime;
If (timeInterval <UPTATE_INTERVAL_TIME ){
Return;
}
LastUpdateTime = currentUpdateTime;
// 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
Float deltaX = x-lastX;
Float deltaY = y-lastY;
Float deltaZ = z-lati;
LastX = x;
LastY = y;
Las-1 = z;
Double speed = (Math. sqrt (deltaX * deltaX + deltaY * deltaY
+ DeltaZ * deltaZ)/timeInterval) * 100;
If (speed> = SPEED_SHRESHOLD ){
Vibrator. vibrate (300 );
Image. setImageResource (R. drawable. running01 );
}
}
@ Override
Public void onAccuracyChanged (Sensor sensor, int accuracy ){
}
};
}