Nowadays, many applications use the function of shaking the mobile phone to change the content, for example, the "Shake" function. To implement this function two days ago, let's take a look at the sensor content.
There are several types of sensors:
Direction sensor: sensor. type_orientation
Acceleration (gravity) sensor: sensor. type_accelerometer
Light Sensor: sensor. type_light
Magnetic Field Sensor: sensor. type_magnetic_field
Distance (proximity) sensor: sensor. type_proximity
Temperature Sensor: sensor. type_temperature
In the mobile phone shake function, we only use the acceleration sensor.
The Unit returned by the accelerometer is the unit of acceleration m/s ^ 2 (meter per second). The values in three directions are
Values [0]: X-axis Acceleration
Values [1]: acceleration in Y-axis direction
Values [2]: acceleration in the z-axis direction
The X, Y, and Z directions are defined as the coordinates of the reference system at the bottom right of the horizontally placed mobile phone.
The Direction X is the horizontal direction of the mobile phone, and the right is positive
The Y direction is the horizontal vertical direction of the mobile phone, and the front is positive
The Y direction is the vertical direction of the mobile phone space, the direction of the sky is positive, and the direction of the earth is negative
Therefore, if your mobile phone is placed in different locations, the acceleration speed varies in three directions. Here, the acceleration in the three directions is different from the acceleration in the traditional sense (9.8 m/s2) and needs to be refined.
The following code clears textview text after shaking the phone:
Package Gy. Lovers;
Import java. util. arraylist;
Import java. util. List;
Import java. util. Random;
Import Android. App. activity;
Import Android. App. Service;
Import Android. content. res. Resources;
Import Android. Hardware. sensor;
Import Android. Hardware. sensorevent;
Import Android. Hardware. sensoreventlistener;
Import Android. Hardware. sensormanager;
Import Android. OS. Bundle;
Import Android. OS. vibrator;
Import Android. View. view;
Import Android. widget. Button;
// The sensoreventlistener interface must be implemented.
Public class loverschise extends activity implements sensoreventlistener {
Button clear;
// Define the sensor Manager
Private sensormanager msensormanager;
// Vibrate
Private vibrator;
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. loverschise );
// Obtain the Sensor Management Service
Msensormanager = (sensormanager) getsystemservice (sensor_service );
// Vibrate
Vibrator = (vibrator) getsystemservice (service. vibrator_service );
Clear = (button) findviewbyid (R. Id. Clear );
Clear. setonclicklistener (New button. onclicklistener (){
@ Override
Public void onclick (view arg0 ){
// Todo auto-generated method stub
// After clicking the button, set text for the button
Clear. settext ("assign a value to the text of the button now ~ ");
});
@ Override
Protected void onresume (){
Super. onresume ();
// Acceleration Sensor
Msensormanager. registerlistener (this,
Msensormanager. getdefasensensor (sensor. type_accelerometer ),
// Also include sensor_delay_ui, sensor_delay_fastest, sensor_delay_game, etc,
// Depending on the application, the desired response rate varies, depending on the actual situation.
Sensormanager. sensor_delay_normal );
}
@ Override
Protected void onstop (){
Msensormanager. unregisterlistener (this );
Super. onstop ();
}
@ Override
Protected void onpause (){
Msensormanager. unregisterlistener (this );
Super. onpause ();
}
@ Override
Public void onaccuracychanged (sensor, int accuracy ){
// Todo auto-generated method stub
// Call back this method when the sensor precision changes. Do nothing.
}
@ Override
Public void onsensorchanged (sensorevent event ){
// Todo auto-generated method stub
Int sensortype = event. sensor. GetType ();
// Values [0]: X axis, values [1]: Y axis, values [2]: Z axis
Float [] values = event. values;
If (sensortype = sensor. type_accelerometer ){
/* Normally, the maximum value of any axis is 9.8 ~ Between 10, only when you suddenly shake your phone
*, The Instantaneous Acceleration will suddenly increase or decrease.
* After actual testing, you only need to monitor the setting you need when the acceleration of any axis is greater than 14.
* OK ~~~
*/
If (math. ABS (Values [0])> 14 | math. ABS (Values [1])> 14 | math. ABS (Values [2])> 14 )){
// After shaking the phone, set the word displayed on the button to null.
Clear. settext (null );
// Shake the phone, and then follow the vibration prompt ~~
Vibrator. vibrate (500 );
}
}
}
}
Reposted from: the shake function implemented by sensors in Android Development