Android "Shake" code implementation

Source: Internet
Author: User

Core interface code:

Package com. hyxf. main;

Import android. content. Context;
Import android. hardware. Sensor;
Import android. hardware. SensorEvent;
Import android. hardware. SensorEventListener;
Import android. hardware. SensorManager;
Import android. util. Log;

Public class ShakeListener implements SensorEventListener {

String TAG = "ShakeListener ";
// Speed threshold, which is effective when the shaking speed reaches this value
Private static final int SPEED_SHRESHOLD = 3000;
// Interval between two checks
Private static final int UPTATE_INTERVAL_TIME = 70;
// Sensor Manager
Private SensorManager sensorManager;
// Sensor
Private Sensor sensor;
// Gravity sensor listener
Private OnShakeListener onShakeListener;
// Context
Private Context mContext;
// Gravity-sensing coordinate at the last position on the mobile phone
Private float lastX;
Private float lastY;
Private float lati;
// Last detection time
Private long lastUpdateTime;
// Constructor
Public ShakeListener (Context c ){
// Obtain the listener object
MContext = c;
Start ();
}
// Start
Public void start (){
// Obtain the sensor Manager
SensorManager = (SensorManager) mContext
. GetSystemService (Context. SENSOR_SERVICE );
If (sensorManager! = Null ){
// Obtain the gravity Sensor
Sensor = sensorManager. getdefasensensor (Sensor. TYPE_ACCELEROMETER );
}
// Register
If (sensor! = Null ){
SensorManager. registerListener (this, sensor,
SensorManager. SENSOR_DELAY_GAME );
}
}
// Stop Detection
Public void stop (){
SensorManager. unregisterListener (this );
}
// Set the gravity sensor listener
Public void setOnShakeListener (OnShakeListener listener ){
OnShakeListener = listener;
}
// The gravity sensor senses the change data.
Public void onSensorChanged (SensorEvent event ){
// Current detection time
Long currentUpdateTime = System. currentTimeMillis ();
// Interval between two checks
Long timeInterval = currentUpdateTime-lastUpdateTime;
// Determine whether the detection interval has been reached
If (timeInterval <UPTATE_INTERVAL_TIME)
Return;
// The current time is changed to the last time
LastUpdateTime = currentUpdateTime;
// Obtain the coordinates of x, y, and z.
Float x = event. values [0];
Float y = event. values [1];
Float z = event. values [2];
// Obtain the variation values of x, y, and z.
Float deltaX = x-lastX;
Float deltaY = y-lastY;
Float deltaZ = z-lati;
// Convert the current coordinates to the last coordinates.
LastX = x;
LastY = y;
Las-1 = z;
Double speed = Math. sqrt (deltaX * deltaX + deltaY * deltaY + deltaZ
* DeltaZ)
/TimeInterval * 10000;
Log. v (TAG, "============= log ====================== ");
// Reach the speed threshold and send a prompt
If (speed> = SPEED_SHRESHOLD ){
OnShakeListener. onShake ();
}
}
Public void onAccuracyChanged (Sensor sensor, int accuracy ){
}
// Shake listener Interface
Public interface OnShakeListener {
Public void onShake ();
}
}

Application code:

Public class yaoAcitivity extends Activity {
ShakeListener mShakeListener = null;
Private TextView TV;
Private ImageView iv;
@ Override
Protected void onCreate (Bundle savedInstanceState ){
// TODO Auto-generated method stub
Super. onCreate (savedInstanceState );

This. setContentView (R. layout. main );
 
TV = (TextView) this. findViewById (R. id. textView1 );
Iv = (ImageView) this. findViewById (R. id. imageView1 );
 
MShakeListener = new ShakeListener (this );
MShakeListener. setOnShakeListener (new shakeLitener ());
}
Private class shakeLitener implements OnShakeListener {

@ Override
Public void onShake (){
// TODO Auto-generated method stub
TV. setText ("Shake it! ");
Iv. setImageResource (R. drawable. attitude_laugh );
MShakeListener. stop ();
}
 
}
}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.