核心代码片段如下:
private Handler handler = new Handler(){
public void handleMessage(Message msg) {
switch (msg.what) {
case SHAKE_START:
isShake = true;//摇动中
startAnimation();
break;
case SHAKE_END://摇动结束
isShake = false;
stopAnimation();
Toast.makeText(TestSensorActivity1.this, "检测到摇晃,执行你想要的操作!", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
super.handleMessage(msg);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.yaoyiyao_test1_layout);
sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
vibrator = (Vibrator) getSystemService(Service.VIBRATOR_SERVICE);
animation_yaoyiyao= (ImageView) findViewById(R.id.yaoyiyao_test1_img);
}
@Override
protected void onResume() {
super.onResume();
// 注册监听器
sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
protected void onPause() {
super.onPause();
sensorManager.unregisterListener(this);
}
@Override
public void onSensorChanged(SensorEvent sensorEvent) {
int sensorType = sensorEvent.sensor.getType();
// values[0]:X轴,values[1]:Y轴,values[2]:Z轴
float[] values = sensorEvent.values;
Log.e("YaoYiYao", "values[0]:" + values[0] + " values[1]:" + values[0] + " values[2]" + values[0]);
if (sensorType == Sensor.TYPE_ACCELEROMETER) {
if ((Math.abs(values[0]) + Math.abs(values[1])+ Math
.abs(values[2])) > 18) {
// 摇动手机后,再伴随震动提示~~
if(!isShake){
vibrator.vibrate(new long[]{200, 300, 200, 300, 200}, -1);
//发送一个空消息,告诉handler已经开始摇动了
handler.sendEmptyMessage(SHAKE_START);
//间隔1.2s,发送一个空消息,告诉handler已经结束摇动了//避免了多次请求处理
handler.sendEmptyMessageDelayed(SHAKE_END, 1200);
}
}
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int i) {
// 当传感器精度改变时回调该方法,Do nothing.
}
资源均来自第三方,谨慎下载,前往第三方网站下载