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 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 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 );
}
}
}
}
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 ).
The above is the code for clearing TextView text after shaking the phone.