Shake, you can monitor the start and finish

Shake on the Android side, which is mainly used in the SensorManager class. Through the gravity sensor, the position change of the xyz three axes is obtained to determine whether it is shaking

1. SensorManager initialization registration SensorManager

//Get the SensorManager responsible for managing the sensor
 mSensorManager = ((SensorManager) getSystemService( SENSOR_SERVICE ) );
 if ( mSensorManager != null ) {
     //Get the acceleration sensor
 mAccelerometerSensor = mSensorManager .getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
     ifull ( mAccelerometer = nullSensor ){
         mSensorManager.registerListener ( this , mAccelerometerSensor , SensorManager. SENSOR_DELAY_UI ;    
    }
}
2. Log off the SensorManager in the onPause method
@Override
protected void onPause() {
     // Be sure to unregister mSensorManager during pause
     // Otherwise, it will cause a bug that still takes effect after the interface exits after shaking
 if ( mSensorManager != null ) {
         mSensorManager .unregisterListener( this );    
    }
    super.onPause();
}
3. Listen to gravity sensing events, let the activity implement SensorEventListener monitoring, and rewrite the onSensorChanged and onAccuracyChanged methods
//This method can get the value of the xyz direction of the mobile phone, and judge whether it is a shaking action based on this value
@Override
public void onSensorChanged(SensorEvent event) {
    int type = event.sensor.getType();

    if (type == Sensor.TYPE_ACCELEROMETER ) {
         //Get three direction values
 ​​float [] values ​​= event.values ​​; float
         x = values[ 0 ];
         float y = values[ 1 ];
         float z = values[ 2 ];        

        if ((Math.abs(x) > 17 || Math.abs(y) > 17 || Math
                .abs (z) > 17 ) && ! isShake ) {
             isShake = true ;
             // TODO: 2016/10/19 Realize the shaking logic, after shaking
 Thread thread = new Thread() {
                 @Override
 public void run() {
                     super .run();
                     try {
                         //Start to vibrate and make a sound to show the animation effect
 mHandler .obtainMessage( START_SHAKE ).sendToTarget();                                                    
                        Thread.sleep ( 500 );
                         //One more vibration prompt
 mHandler .obtainMessage( AGAIN_SHAKE ) .sendToTarget();                        
                        Thread.sleep(500);
                        mHandler.obtainMessage(END_SHAKE).sendToTarget();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            };
            thread.start();
        }
    }
}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {

}
Fourth, the Handler handles the shaking start and completion events, and all processing logic.
 
 
private class MyHandler extends Handler { private WeakReference<ChristmasWebActivity> mReference; private ChristmasWebActivity mActivity; public MyHandler(ChristmasWebActivity activity) { mReference = new WeakReference<ChristmasWebActivity>(activity); if (mReference != null) { mActivity = mReference.get(); } } @Override  public void handleMessage(Message msg) { super.handleMessage(msg); switch (msg.what ) { case START_SHAKE : // start shaking break ; case AGAIN_SHAKE : // continue shaking without processing // mActivity.mVibrator.vibrate(300); break ; case END_SHAKE : // end shaking the overall effect is over, will Shake is set to false mActivity . isShake = false ; // Show the effect of the upper and lower pictures coming back // Start the animation after the end. break ; } } }      

Note: Phone vibration needs to add vibration permission

<uses-permission android:name="android.permission.VIBRATE"></uses-permission>
 

Guess you like

Origin blog.csdn.net/lzq520210/article/details/53581974