x,y方向则没有上述限制。
代码:
package com.czz.test; import android.content.Context; import android.hardware.Sensor; import android.hardware.SensorEvent; import android.hardware.SensorEventListener; import android.hardware.SensorListener; import android.hardware.SensorManager; import android.os.Vibrator; import android.util.Log; public class SensorUtils { static final String TAG = "SensorUtis"; private static TesttttActivity mContext; private static SensorUtils mSingleInstance; private SensorEventListener mSensorEventListener; private SensorListener mSensorListener; Vibrator mVibrator; SensorManager mSensorManager; Sensor mSensor; public SensorUtils(TesttttActivity context) { this.mContext = context; // TODO Auto-generated constructor stub } public static SensorUtils getInstance(TesttttActivity context) { synchronized(SensorUtils.class) { if(mSingleInstance == null) { mSingleInstance = new SensorUtils(context); } } mContext = context; return mSingleInstance; } private void setSensorListener() { mSensorManager = (SensorManager) mContext.getSystemService(Context.SENSOR_SERVICE); mVibrator = (Vibrator) mContext.getSystemService(Context.VIBRATOR_SERVICE); mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); mSensorEventListener = new SensorEventListener() { float x, y, z, lastX, lastY; long lastT, lastST; @Override public void onAccuracyChanged(Sensor sensor, int accuracy) { // TODO Auto-generated method stub } @Override public void onSensorChanged(SensorEvent event) { // TODO Auto-generated method stub long currT = System.currentTimeMillis(); if((currT - lastT) > 100) { long diffT = (currT - lastT); lastT = currT; x = event.values[SensorManager.DATA_X]; y = event.values[SensorManager.DATA_Y]; z = event.values[SensorManager.DATA_Z]; Log.i(TAG, "x="+x+" y="+y+"z="+z); float axis = 0; if(lastX != 0) { axis = Math.abs((x+y-lastX-lastY))/diffT*10000; } if(axis > 1200 && (currT - lastST) > 200) { lastST = currT; mVibrator.vibrate(new long[] {500,100,1000}, -1); mContext.refreshView(); } lastX = x; lastY = y; } } }; mSensorManager.registerListener(mSensorEventListener, mSensor, SensorManager.SENSOR_DELAY_GAME); } private void setShakeListener() { mSensorManager = (SensorManager) mContext.getSystemService(Context.SENSOR_SERVICE); mVibrator = (Vibrator) mContext.getSystemService(Context.VIBRATOR_SERVICE); mSensorListener = new SensorListener() { long lastUpdate, lastShakeTime = 0; static final int SHAKE_THRESHOLD = 1200; float x, y, last_x = 0, last_y = 0; public void onAccuracyChanged(int sensor, int accuracy) { // TODO Auto-generated method stub } public void onSensorChanged(int sensor, float[] values) { if (sensor == SensorManager.SENSOR_ACCELEROMETER) { long curTime = System.currentTimeMillis(); if ((curTime - lastUpdate) > 100) { long diffTime = (curTime - lastUpdate); lastUpdate = curTime; x = values[SensorManager.DATA_X]; y = values[SensorManager.DATA_Y]; float acceChangeRate = 0; if (last_x != 0) { acceChangeRate = Math.abs(x + y - last_x - last_y) / diffTime * 10000; } if (acceChangeRate > SHAKE_THRESHOLD && curTime - lastShakeTime > 200) { lastShakeTime = curTime; mVibrator.vibrate(new long[] { 500, 100, 1000 }, -1); mContext.refreshView(); } last_x = x; last_y = y; } } } }; mSensorManager.registerListener(mSensorListener, SensorManager.SENSOR_ACCELEROMETER); } /* RK_ID: , Aut: */ public void registerShakeListener() { Log.i(TAG, "registerShakeListener >>>>>>>>> "); if (mContext == null) { return; } // setShakeListener(); setSensorListener(); } /* RK_ID: , Aut: */ public void resetShakeListener() { Log.i(TAG, "resetShakeListener >>>>>>>>> "); if (mContext == null) return; if (null != mVibrator) { mVibrator.cancel(); mVibrator = null; } if (null != mSensorManager) { mSensorManager.unregisterListener(mSensorListener, SensorManager.SENSOR_ACCELEROMETER); } } }