实现“摇一摇”功能,其实很简单,就是检测手机的重力感应,具体实现代码如下:
1、在 AndroidManifest.xml 中添加操作权限
2、实现代码
package com.xs.test; import android.app.Activity; import android.hardware.Sensor; import android.hardware.SensorEvent; import android.hardware.SensorEventListener; import android.hardware.SensorManager; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.os.Vibrator; import android.util.Log; import android.widget.Toast; /** * 安卓晃动手机监听--“摇一摇” * * @author 单红宇 * */ public class TestSensorActivity extends Activity { private SensorManager sensorManager; private Vibrator vibrator; private static final String TAG = "TestSensorActivity"; private static final int SENSOR_SHAKE = 10; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE); vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE); } @Override protected void onResume() { super.onResume(); if (sensorManager != null) {// 注册监听器 sensorManager.registerListener(sensorEventListener, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL); // 第一个参数是Listener,第二个参数是所得传感器类型,第三个参数值获取传感器信息的频率 } } @Override protected void onPause() { super.onPause(); if (sensorManager != null) {// 取消监听器 sensorManager.unregisterListener(sensorEventListener); } } /** * 重力感应监听 */ private SensorEventListener sensorEventListener = new SensorEventListener() { @Override public void onSensorChanged(SensorEvent event) { // 传感器信息改变时执行该方法 float[] values = event.values; float x = values[0]; // x轴方向的重力加速度,向右为正 float y = values[1]; // y轴方向的重力加速度,向前为正 float z = values[2]; // z轴方向的重力加速度,向上为正 Log.i(TAG, "x轴方向的重力加速度" + x + ";y轴方向的重力加速度" + y + ";z轴方向的重力加速度" + z); // 一般在这三个方向的重力加速度达到40就达到了摇晃手机的状态。 int medumValue = 19;// 三星 i9250怎么晃都不会超过20,没办法,只设置19了 if (Math.abs(x) > medumValue || Math.abs(y) > medumValue || Math.abs(z) > medumValue) { vibrator.vibrate(200); Message msg = new Message(); msg.what = SENSOR_SHAKE; handler.sendMessage(msg); } } @Override public void onAccuracyChanged(Sensor sensor, int accuracy) { } }; /** * 动作执行 */ Handler handler = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); switch (msg.what) { case SENSOR_SHAKE: Toast.makeText(TestSensorActivity.this, "检测到摇晃,执行操作!", Toast.LENGTH_SHORT).show(); Log.i(TAG, "检测到摇晃,执行操作!"); break; } } }; }
这种摇一摇比较普通,没有经过算法的加强,重力加速度就是10。
算法优化如下
package com.example.url; import java.io.IOException; import java.net.URL; import com.hahashijie.imageload.ImageLoader; import android.annotation.SuppressLint; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.drawable.Drawable; import android.hardware.Sensor; import android.hardware.SensorEvent; import android.hardware.SensorEventListener; import android.hardware.SensorManager; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.os.Vibrator; import android.util.Log; import android.widget.ImageView; @SuppressLint("HandlerLeak") public class MainActivity extends Activity { private ImageView image; private SensorManager sensorManager; private Sensor sensor; private Vibrator vibrator; private static final int UPTATE_INTERVAL_TIME = 50; private static final int SPEED_SHRESHOLD = 30;//这个值调节灵敏度 private long lastUpdateTime; private float lastX; private float lastY; private float lastZ; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE); vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE); image = (ImageView) findViewById(R.id.image); } @Override protected void onResume() { // TODO Auto-generated method stub super.onResume(); if (sensorManager != null) { sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); } if (sensor != null) { sensorManager.registerListener(sensorEventListener, sensor, SensorManager.SENSOR_DELAY_GAME);//这里选择感应频率 } } /** * 重力感应监听 */ private SensorEventListener sensorEventListener = new SensorEventListener() { @Override public void onSensorChanged(SensorEvent event) { long currentUpdateTime = System.currentTimeMillis(); long timeInterval = currentUpdateTime - lastUpdateTime; if (timeInterval= SPEED_SHRESHOLD) { vibrator.vibrate(300); image.setImageResource(R.drawable.running01); } } @Override public void onAccuracyChanged(Sensor sensor, int accuracy) { } }; }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。