檢測程式碼
public static abstract class a implements SensorEventListener {
private static final String LG_E510 = "LG-E510";
private static final String TAG = "MicroMsg.ShakeSensorListener";
private static int THREAHOLD;
private float[] mPrev = new float[3];
private final float[] mScale = {2.0f, 2.5f, 0.5f};
public abstract void onRelease();
public abstract void onShake(boolean z);
public void onShake(float[] fArr) {
}
public void reset() {
C8541ad.m11821d("MicroMsg.ShakeSensorListener", "reset threadHold");
THREAHOLD = 5;
if (Build.MODEL.equals("LG-E510")) {
THREAHOLD = 4;
}
}
static {
THREAHOLD = 5;
if (Build.MODEL.equals("LG-E510")) {
THREAHOLD = 4;
}
}
private float selfAdaptation(float f) {
if (f >= 4.0f) {
C8541ad.m11831v("MicroMsg.ShakeSensorListener", "result:" + f + " THREAHOLD:" + THREAHOLD);
}
if (THREAHOLD < 9) {
if (f >= 14.0f) {
THREAHOLD = 9;
} else {
int i = (int) f;
if (THREAHOLD < i - 4) {
THREAHOLD = i - 4;
}
}
}
return f;
}
public void onSensorChanged(SensorEvent sensorEvent) {
float[] fArr = new float[3];
float[] fArr2 = sensorEvent.values;
boolean z = false;
for (int i = 0; i < 3; i++) {
fArr[i] = (float) Math.round(this.mScale[i] * (fArr2[i] - this.mPrev[i]) * 0.45f);
if (selfAdaptation(Math.abs(fArr[i])) > ((float) THREAHOLD)) {
z = true;
}
this.mPrev[i] = fArr2[i];
}
if (z) {
C8541ad.m11821d("MicroMsg.ShakeSensorListener", "sensorChanged " + sensorEvent.sensor.getName() + " (" + fArr2[0] + ", " + fArr2[1] + ", " + fArr2[2] + ") diff(" + fArr[0] + " " + fArr[1] + " " + fArr[2] + ")");
onShake(true);
onShake(sensorEvent.values);
}
}
public void onAccuracyChanged(Sensor sensor, int i) {
}
}
封裝好的工具類
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Build;
import android.util.Log;
public class WxShakeHelper {
private Context mContext;
private SensorManager mSensorManager;
private Sensor mSensor;
private BaseListener listener;
public WxShakeHelper(Context mContext,BaseListener baseListener) {
this.mContext = mContext;
this.listener=baseListener;
}
public void startMonitor() {
mSensorManager = (SensorManager) mContext.getSystemService(Context.SENSOR_SERVICE);
if (mSensorManager != null) {
mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
}
if (mSensor != null) {
if (mSensorManager != null) {
mSensorManager.registerListener(this.listener, mSensor, SensorManager.SENSOR_DELAY_GAME);
}
}
}
public void stopMonitor() {
mSensorManager.unregisterListener(this.listener);
}
public static abstract class BaseListener implements SensorEventListener {
private static final String LG_E510 = "LG-E510";
public static final String TAG = "ShakeSensorListener";
private static int THREAHOLD;
private float[] mPrev = new float[3];
private final float[] mScale = {2.0f, 2.5f, 0.5f};
public abstract void onRelease();
public abstract void onShake(boolean z);
public void onShake(float[] fArr) {
}
public void reset() {
Log.i(TAG, "reset threadHold");
THREAHOLD = 5;
if (Build.MODEL.equals("LG-E510")) {
THREAHOLD = 4;
}
}
static {
THREAHOLD = 5;
if (Build.MODEL.equals("LG-E510")) {
THREAHOLD = 4;
}
}
private float selfAdaptation(float f) {
if (f >= 4.0f) {
Log.i(TAG, "result:" + f + " THREAHOLD:" + THREAHOLD);
}
if (THREAHOLD < 9) {
if (f >= 14.0f) {
THREAHOLD = 9;
} else {
int i = (int) f;
if (THREAHOLD < i - 4) {
THREAHOLD = i - 4;
}
}
}
return f;
}
public void onSensorChanged(SensorEvent sensorEvent) {
float[] fArr = new float[3];
float[] fArr2 = sensorEvent.values;
boolean z = false;
for (int i = 0; i < 3; i++) {
fArr[i] = (float) Math.round(this.mScale[i] * (fArr2[i] - this.mPrev[i]) * 0.45f);
if (selfAdaptation(Math.abs(fArr[i])) > ((float) THREAHOLD)) {
z = true;
}
this.mPrev[i] = fArr2[i];
}
if (z) {
Log.i(TAG, "sensorChanged " + sensorEvent.sensor.getName() + " (" + fArr2[0] + ", " + fArr2[1] + ", " + fArr2[2] + ") diff(" + fArr[0] + " " + fArr[1] + " " + fArr[2] + ")");
onShake(true);
onShake(sensorEvent.values);
}
}
public void onAccuracyChanged(Sensor sensor, int i) {
}
}
呼叫
new WxShakeHelper(this, new WxShakeHelper.BaseListener() {
@Override
public void onRelease() {
}
@Override
public void onShake(boolean z) {
}
}).startMonitor();