Android realizes the simple function of shaking

  • 2021-12-12 05:37:09
  • OfStack

When talking about Shake 1, you may think of WeChat's Shake 1 function. Later, many APP also followed the related functions of Shake 1. Here we will introduce how to do it with one shake and one shake.

Step 1: Declare an SensorManager object

Step 2: Override the onResume method of Activity, where the sensor listening event is registered and the type of sensor to be listened to is specified.

Step 3: Override the onPause method of Activity, where the sensor event is unregistered

Step 4: Write a sensor event listener that inherits from SensorEventListener and implements both onSensorChanged and onAccuracyChanged. Among them, the first method triggers when sensing information changes, and the business logic is processed here; The last 1 method is triggered when the longitude changes, and 1 method generally does not need to be processed.

Code Sample

Add Permissions


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

MainActivity.java


public class MainActivity extends BaseActivity implements SensorEventListener {
 
  private TextView tv_sensor;
  private SensorManager mSensorMgr;// Declaration 1 Sensor manager objects 
  private Vibrator mVibrator;// Declaration 1 Vibrator objects 
 
  @Override
  protected MvcBaseModel getModelImp() {
    return null;
  }
 
  @Override
  protected int getContentLayoutId() {
    return R.layout.activity_main;
  }
 
  @Override
  protected void initWidget() {
    tv_sensor = findViewById(R.id.tv_sensor);
    // Getting Sensor Manager Objects from System Services 
    mSensorMgr = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    // Getting a vibrator object from a system service 
    mVibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
  }
 
  @Override
  protected void onPause() {
    super.onPause();
    mSensorMgr.unregisterListener(this);
  }
 
  @Override
  protected void onResume() {
    super.onResume();
    mSensorMgr.registerListener(this
        ,mSensorMgr.getDefaultSensor(Sensor.TYPE_ACCELEROMETER)
        ,SensorManager.SENSOR_DELAY_NORMAL);
  }
 
  @Override
  public void onSensorChanged(SensorEvent event) {
    if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER){// Acceleration change event 
      //value[0]:X Shaft ,value[1]:Y Axis, values[2]:Z Shaft 
      float[] values = event.values;
      if ((Math.abs(values[0])>15) || Math.abs(values[1])>15 || Math.abs(values[2])>15){
        tv_sensor.setText(" Congratulations on shaking 1 Shake "+System.currentTimeMillis());
        // System detection shake 1 After shaking the event, vibrate the mobile phone to prompt the user 
        mVibrator.vibrate(500);
      }
    }
  }
 
  // Callback the method when the sensor accuracy changes, 1 There is no need to deal with it 
  @Override
  public void onAccuracyChanged(Sensor sensor, int accuracy) {
 
  }
}

In actual use, I found that the system will have the problem of multiple callbacks after shaking 1. Here we can lock and no longer respond after one response. Continue to respond after 3 seconds. In this way, only one related operation can be performed.


Related articles: