实现“摇一摇”功能,其实很简单,就是检测手机的重力感应,具体实现代码如下: 一、在 AndroidManifest.xml 中添加操作权限 二、实现代码
1 package com.xs.test; 2 3 import android.app.Activity; 4 import android.hardware.Sensor; 5 import android.hardware.SensorEvent; 6 import android.hardware.SensorEventListener; 7 import android.hardware.SensorManager; 8 import android.os.Bundle; 9 import android.os.Handler; 10 import android.os.Message; 11 import android.os.Vibrator; 12 import android.util.Log; 13 import android.widget.Toast; 14 15 /** 16 * 安卓晃动手机监听--“摇一摇” 17 * 18 * @author 单红宇 19 * 20 */ 21 public class TestSensorActivity extends Activity { 22 23 private SensorManager sensorManager; 24 private Vibrator vibrator; 25 26 private static final String TAG = "TestSensorActivity"; 27 private static final int SENSOR_SHAKE = 10; 28 29 /** Called when the activity is first created. */ 30 @Override 31 public void onCreate(Bundle savedInstanceState) { 32 super.onCreate(savedInstanceState); 33 setContentView(R.layout.main); 34 sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE); 35 vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE); 36 } 37 38 @Override 39 protected void onResume() { 40 super.onResume(); 41 if (sensorManager != null) {// 注册监听器 42 sensorManager.registerListener(sensorEventListener, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL); 43 // 第一个参数是Listener,第二个参数是所得传感器类型,第三个参数值获取传感器信息的频率 44 } 45 } 46 47 @Override 48 protected void onPause() { 49 super.onPause(); 50 if (sensorManager != null) {// 取消监听器 51 sensorManager.unregisterListener(sensorEventListener); 52 } 53 } 54 55 /** 56 * 重力感应监听 57 */ 58 private SensorEventListener sensorEventListener = new SensorEventListener() { 59 60 @Override 61 public void onSensorChanged(SensorEvent event) { 62 // 传感器信息改变时执行该方法 63 float[] values = event.values; 64 float x = values[0]; // x轴方向的重力加速度,向右为正 65 float y = values[1]; // y轴方向的重力加速度,向前为正 66 float z = values[2]; // z轴方向的重力加速度,向上为正 67 Log.i(TAG, "x轴方向的重力加速度" + x + ";y轴方向的重力加速度" + y + ";z轴方向的重力加速度" + z); 68 // 一般在这三个方向的重力加速度达到40就达到了摇晃手机的状态。 69 int medumValue = 19;// 三星 i9250怎么晃都不会超过20,没办法,只设置19了 70 if (Math.abs(x) > medumValue || Math.abs(y) > medumValue || Math.abs(z) > medumValue) { 71 vibrator.vibrate(200); 72 Message msg = new Message(); 73 msg.what = SENSOR_SHAKE; 74 handler.sendMessage(msg); 75 } 76 } 77 78 @Override 79 public void onAccuracyChanged(Sensor sensor, int accuracy) { 80 81 } 82 }; 83 84 /** 85 * 动作执行 86 */ 87 Handler handler = new Handler() { 88 89 @Override 90 public void handleMessage(Message msg) { 91 super.handleMessage(msg); 92 switch (msg.what) { 93 case SENSOR_SHAKE: 94 Toast.makeText(TestSensorActivity.this, "检测到摇晃,执行操作!", Toast.LENGTH_SHORT).show(); 95 Log.i(TAG, "检测到摇晃,执行操作!"); 96 break; 97 } 98 } 99 100 }; 101 } 102 这种摇一摇比较普通,没有经过算法的加强,重力加速度就是10。 103 算法优化如下 104 package com.example.url; 105 106 107 import java.io.IOException; 108 import java.net.URL; 109 110 111 import com.hahashijie.imageload.ImageLoader; 112 113 114 import android.annotation.SuppressLint; 115 import android.app.Activity; 116 import android.graphics.Bitmap; 117 import android.graphics.BitmapFactory; 118 import android.graphics.drawable.Drawable; 119 import android.hardware.Sensor; 120 import android.hardware.SensorEvent; 121 import android.hardware.SensorEventListener; 122 import android.hardware.SensorManager; 123 import android.os.Bundle; 124 import android.os.Handler; 125 import android.os.Message; 126 import android.os.Vibrator; 127 import android.util.Log; 128 import android.widget.ImageView; 129 130 131 @SuppressLint("HandlerLeak") 132 public class MainActivity extends Activity { 133 134 135 private ImageView image; 136 private SensorManager sensorManager; 137 private Sensor sensor; 138 private Vibrator vibrator; 139 private static final int UPTATE_INTERVAL_TIME = 50; 140 private static final int SPEED_SHRESHOLD = 30;//这个值调节灵敏度 141 private long lastUpdateTime; 142 private float lastX; 143 private float lastY; 144 private float lastZ; 145 @Override 146 protected void onCreate(Bundle savedInstanceState) { 147 super.onCreate(savedInstanceState); 148 setContentView(R.layout.activity_main); 149 sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE); 150 vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE); 151 image = (ImageView) findViewById(R.id.image); 152 153 154 } 155 156 157 @Override 158 protected void onResume() { 159 // TODO Auto-generated method stub 160 super.onResume(); 161 if (sensorManager != null) { 162 sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); 163 } 164 if (sensor != null) { 165 sensorManager.registerListener(sensorEventListener, 166 sensor, 167 SensorManager.SENSOR_DELAY_GAME);//这里选择感应频率 168 } 169 } 170 171 172 /** 173 * 重力感应监听 174 */ 175 private SensorEventListener sensorEventListener = new SensorEventListener() { 176 177 178 @Override 179 public void onSensorChanged(SensorEvent event) { 180 long currentUpdateTime = System.currentTimeMillis(); 181 long timeInterval = currentUpdateTime - lastUpdateTime; 182 if (timeInterval < UPTATE_INTERVAL_TIME) { 183 return; 184 } 185 lastUpdateTime = currentUpdateTime; 186 // 传感器信息改变时执行该方法 187 float[] values = event.values; 188 float x = values[0]; // x轴方向的重力加速度,向右为正 189 float y = values[1]; // y轴方向的重力加速度,向前为正 190 float z = values[2]; // z轴方向的重力加速度,向上为正 191 float deltaX = x - lastX; 192 float deltaY = y - lastY; 193 float deltaZ = z - lastZ; 194 195 196 lastX = x; 197 lastY = y; 198 lastZ = z; 199 double speed = (Math.sqrt(deltaX * deltaX + deltaY * deltaY 200 + deltaZ * deltaZ) / timeInterval) * 100; 201 if (speed >= SPEED_SHRESHOLD) { 202 vibrator.vibrate(300); 203 image.setImageResource(R.drawable.running01); 204 } 205 } 206 207 208 @Override 209 public void onAccuracyChanged(Sensor sensor, int accuracy) { 210 211 } 212 }; 213 214 }