1. importandroid.content.Context;
2. importandroid.hardware.Sensor;
3. importandroid.hardware.SensorEvent;
4. importandroid.hardware.SensorEventListener;
5. importandroid.hardware.SensorManager;
6.
7.
8. publicclassShakeListener implementsSensorEventListener {
9. //速度阈值,当摇晃速度达到这值后产生作用
10. privatestaticfinalintSPEED_SHRESHOLD = 4000;
11. //两次检测的时间间隔
12. privatestaticfinalintUPTATE_INTERVAL_TIME = 70;
13.
14. //传感器管理器
15. privateSensorManager sensorManager;
16. //传感器
17. privateSensor sensor;
18. //重力感应监听器
19. privateOnShakeListener onShakeListener;
20. //上下文
21. privateContext context;
22. //手机上一个位置时重力感应坐标
23. privatefloatlastX;
24. privatefloatlastY;
25. privatefloatlastZ;
26.
27. //上次检测时间
28. privatelonglastUpdateTime;
29.
30. //构造器
31. publicShakeListener(Context c) {
32. //获得监听对象
33. context = c;
34. start();
35. }
36.
37. //开始
38. publicvoidstart() {
39. //获得传感器管理器
40. sensorManager = (SensorManager)context.getSystemService(Context.SENSOR_SERVICE);
41. if(sensorManager != null) {
42. //获得重力传感器
43. sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
44. }
45. //注册
46. if(sensor != null) {
47. sensorManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_GAME);
48. }
49.
50. }
51.
52. //停止检测
53. publicvoidstop() {
54. sensorManager.unregisterListener(this);
55. }
56.
57. //摇晃监听接口
58. publicinterfaceOnShakeListener {
59. publicvoidonShake();
60. }
61.
62. //设置重力感应监听器
63. publicvoidsetOnShakeListener(OnShakeListener listener) {
64. onShakeListener = listener;
65. }
66.
67.
68. //重力感应器感应获得变化数据
69. publicvoidonSensorChanged(SensorEvent event) {
70. //现在检测时间
71. longcurrentUpdateTime = System.currentTimeMillis();
72. //两次检测的时间间隔
73. longtimeInterval = currentUpdateTime - lastUpdateTime;
74. //判断是否达到了检测时间间隔
75. if(timeInterval < UPTATE_INTERVAL_TIME)
76. return;
77. //现在的时间变成last时间
78. lastUpdateTime = currentUpdateTime;
79.
80. //获得x,y,z坐标
81. floatx = event.values[0];
82. floaty = event.values[1];
83. floatz = event.values[2];
84.
85. //获得x,y,z的变化值
86. floatdeltaX = x - lastX;
87. floatdeltaY = y - lastY;
88. floatdeltaZ = z - lastZ;
89.
90. //将现在的坐标变成last坐标
91. lastX = x;
92. lastY = y;
93. lastZ = z;
94.
95. doublespeed = Math.sqrt(deltaX*deltaX + deltaY*deltaY + deltaZ*deltaZ)/timeInterval * 10000;
96. //达到速度阀值,发出提示
97. if(speed >= SPEED_SHRESHOLD)
98. onShakeListener.onShake();
99. }
100.
101. publicvoidonAccuracyChanged(Sensor sensor, intaccuracy) {
102.
103. }
104.
105. }
用法:
1. ShakeListener shakeListener = newShakeListener(this);//创建一个对象
2. shakeListener.setOnShakeListener(newOnShakeListener(){//调用setOnShakeListener方法进行监听
3.
4. publicvoidonShake() {
5. //对手机摇晃后的处理(如换歌曲,换图片,震动……)
6. //onVibrator();
7. }
8.
9. });
//震动
1. privatevoidonVibrator() {
2. Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
3. if(vibrator == null) {
4. Vibrator localVibrator = (Vibrator) context.getApplicationContext()
5. .getSystemService("vibrator");
6. vibrator = localVibrator;
7. }
8. vibrator.vibrate(100L);
9. }
标签:晃动,Vibrator,importandroid,SensorEventListener,sensorManager,vibrator,context,sen
From: https://blog.51cto.com/u_548275/6237987