/**
* 加速度传感器,模拟微信摇一摇
*/
public class ActAccelerateSensor extends AppCompatActivity{
private SensorManager sensorManager;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_act_acceleratesensor);
sensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
Sensor sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
sensorManager.registerListener(listener, sensor, SensorManager.SENSOR_DELAY_NORMAL);
}
private SensorEventListener listener = new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent event) {
//加速度可能会是负值,所以要取它们的绝对值
float xValue = Math.abs(event.values[0]);
float yValue = Math.abs(event.values[1]);
float zValue = Math.abs(event.values[2]);
//由于手机在静止的情况下,某一个轴上的加速度也有可能达到9.8m/s2,
//因此这个预定值必定要大于9.8m/s2才行,这里我们设定为15m/s2
if(xValue > 15 || yValue > 15 || zValue > 15){
//认为用户摇动了手机,触发摇一摇逻辑
Toast.makeText(ActAccelerateSensor.this,"摇一摇",Toast.LENGTH_SHORT).show();
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
};
@Override
protected void onDestroy() {
super.onDestroy();
if(sensorManager != null){
sensorManager.unregisterListener(listener);
}
}
}
传感器之加速度:Sensor.TYPE_ACCELEROMETER
最新推荐文章于 2021-05-29 05:54:16 发布