Java android中的连续抖动信号

Java android中的连续抖动信号,java,android,android-sensors,shake,sensormanager,Java,Android,Android Sensors,Shake,Sensormanager,我创建了一个用于震动检测的类和一个用于震动服务的类,以便在检测到震动时进行操作。当我运行服务时,我不断地抖动信号。谁能告诉我问题出在哪里 摇动服务如下: import android.app.Service; import android.content.Context; import android.content.Intent; import android.hardware.Sensor; import android.hardware.SensorManager; import andr

我创建了一个用于震动检测的类和一个用于震动服务的类,以便在检测到震动时进行操作。当我运行服务时,我不断地抖动信号。谁能告诉我问题出在哪里

摇动服务如下:

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.os.IBinder;
import android.support.annotation.Nullable;

/**
 * Created by DELL WORLD on 5/31/2017.
 */

public class ShakeService extends Service {
    private SensorManager mSensorManager;
    private Sensor mAccelerometer;
    private ShakeDetector mShakeDetector;


    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        System.out.println("Shake Service Started");
        // ShakeDetector initialization
        mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        mAccelerometer = mSensorManager
                .getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        mShakeDetector = new ShakeDetector();
        mShakeDetector.setOnShakeListener(new ShakeDetector.OnShakeListener() {

            @Override
            public void onShake(int count) {
                System.out.println("Shaken");



            }
        });
        mSensorManager.registerListener(mShakeDetector, mAccelerometer, SensorManager.SENSOR_DELAY_UI);

    }

    @Override
    public void onDestroy() {
        mSensorManager.unregisterListener(mShakeDetector);
        super.onDestroy();
        System.out.println("Shake Service Destroyed");
    }
}
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.util.FloatMath;

/**
 * Created by DELL WORLD on 5/5/2017.
 */

public class ShakeDetector implements SensorEventListener {
    private SensorManager mSensorManager;
    private Sensor mAccelerometer;
    private ShakeDetector mShakeDetector;
    /*
  * The gForce that is necessary to register as shake.
  * Must be greater than 1G (one earth gravity unit).
  * You can install "G-Force", by Blake La Pierre
  * from the Google Play Store and run it to see how
  *  many G's it takes to register a shake
  */
    private static final float SHAKE_THRESHOLD_GRAVITY = 2.7F;
    private static final int SHAKE_SLOP_TIME_MS = 500;
    private static final int SHAKE_COUNT_RESET_TIME_MS = 3000;

    private OnShakeListener mListener;
    private long mShakeTimestamp;
    private int mShakeCount;

    public void setOnShakeListener(OnShakeListener listener) {
        this.mListener = listener;
    }

    public interface OnShakeListener {
        public void onShake(int count);
    }


    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
        // ignore
    }

    @Override
    public void onSensorChanged(SensorEvent event) {

        if (mListener != null) {
            float x = event.values[0];
            float y = event.values[1];
            float z = event.values[2];

            float gX = x / SensorManager.GRAVITY_EARTH;
            float gY = y / SensorManager.GRAVITY_EARTH;
            float gZ = z / SensorManager.GRAVITY_EARTH;

            // gForce will be close to 1 when there is no movement.
            //float gForce = FloatMath.sqrt(gX * gX + gY * gY + gZ * gZ);
            float gForce = Math.abs(x + y + z - gX - gY - gZ);
            if (gForce > SHAKE_THRESHOLD_GRAVITY) {
                final long now = System.currentTimeMillis();
                // ignore shake events too close to each other (500ms)
                if (mShakeTimestamp + SHAKE_SLOP_TIME_MS > now) {
                    return;
                }

                // reset the shake count after 3 seconds of no shakes
                if (mShakeTimestamp + SHAKE_COUNT_RESET_TIME_MS < now) {
                    mShakeCount = 0;
                }

                mShakeTimestamp = now;
                mShakeCount++;

                mListener.onShake(mShakeCount);
            }
        }
    }
}
和震动检测器,如下所示:

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.os.IBinder;
import android.support.annotation.Nullable;

/**
 * Created by DELL WORLD on 5/31/2017.
 */

public class ShakeService extends Service {
    private SensorManager mSensorManager;
    private Sensor mAccelerometer;
    private ShakeDetector mShakeDetector;


    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        System.out.println("Shake Service Started");
        // ShakeDetector initialization
        mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        mAccelerometer = mSensorManager
                .getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        mShakeDetector = new ShakeDetector();
        mShakeDetector.setOnShakeListener(new ShakeDetector.OnShakeListener() {

            @Override
            public void onShake(int count) {
                System.out.println("Shaken");



            }
        });
        mSensorManager.registerListener(mShakeDetector, mAccelerometer, SensorManager.SENSOR_DELAY_UI);

    }

    @Override
    public void onDestroy() {
        mSensorManager.unregisterListener(mShakeDetector);
        super.onDestroy();
        System.out.println("Shake Service Destroyed");
    }
}
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.util.FloatMath;

/**
 * Created by DELL WORLD on 5/5/2017.
 */

public class ShakeDetector implements SensorEventListener {
    private SensorManager mSensorManager;
    private Sensor mAccelerometer;
    private ShakeDetector mShakeDetector;
    /*
  * The gForce that is necessary to register as shake.
  * Must be greater than 1G (one earth gravity unit).
  * You can install "G-Force", by Blake La Pierre
  * from the Google Play Store and run it to see how
  *  many G's it takes to register a shake
  */
    private static final float SHAKE_THRESHOLD_GRAVITY = 2.7F;
    private static final int SHAKE_SLOP_TIME_MS = 500;
    private static final int SHAKE_COUNT_RESET_TIME_MS = 3000;

    private OnShakeListener mListener;
    private long mShakeTimestamp;
    private int mShakeCount;

    public void setOnShakeListener(OnShakeListener listener) {
        this.mListener = listener;
    }

    public interface OnShakeListener {
        public void onShake(int count);
    }


    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
        // ignore
    }

    @Override
    public void onSensorChanged(SensorEvent event) {

        if (mListener != null) {
            float x = event.values[0];
            float y = event.values[1];
            float z = event.values[2];

            float gX = x / SensorManager.GRAVITY_EARTH;
            float gY = y / SensorManager.GRAVITY_EARTH;
            float gZ = z / SensorManager.GRAVITY_EARTH;

            // gForce will be close to 1 when there is no movement.
            //float gForce = FloatMath.sqrt(gX * gX + gY * gY + gZ * gZ);
            float gForce = Math.abs(x + y + z - gX - gY - gZ);
            if (gForce > SHAKE_THRESHOLD_GRAVITY) {
                final long now = System.currentTimeMillis();
                // ignore shake events too close to each other (500ms)
                if (mShakeTimestamp + SHAKE_SLOP_TIME_MS > now) {
                    return;
                }

                // reset the shake count after 3 seconds of no shakes
                if (mShakeTimestamp + SHAKE_COUNT_RESET_TIME_MS < now) {
                    mShakeCount = 0;
                }

                mShakeTimestamp = now;
                mShakeCount++;

                mListener.onShake(mShakeCount);
            }
        }
    }
}
导入android.hardware.Sensor;
导入android.hardware.SensorEvent;
导入android.hardware.SensorEventListener;
导入android.hardware.SensorManager;
导入android.util.FloatMath;
/**
*由戴尔世界于2017年5月5日创建。
*/
公共类ShakeDetector实现SensorEventListener{
私人传感器管理器;
专用传感器mAccelerometer;
专用震动检测器mShakeDetector;
/*
*注册为震动所需的G力。
*必须大于1G(一个地球重力单位)。
*你可以安装Blake La Pierre的“G-Force”
*从Google Play商店下载并运行它,看看如何
*记录一次震动需要很多G
*/
专用静态最终浮子震动阈值重力=2.7F;
专用静态最终整数抖动斜率时间=500;
专用静态最终整数抖动\计数\重置\时间\毫秒=3000;
私人OnShakeListener-mListener;
私人长mShakeTimestamp;
私人帐户;
公共无效设置OnShakeListener(OnShakeListener侦听器){
this.mListener=侦听器;
}
ShakeListener上的公共接口{
公共无效onShake(整数计数);
}
@凌驾
精度更改时的公共无效(传感器,int精度){
//忽略
}
@凌驾
传感器更改时的公共无效(传感器事件){
if(mListener!=null){
float x=事件值[0];
浮动y=事件值[1];
float z=事件值[2];
浮子gX=x/SensorManager.GRAVITY\u接地;
浮子gY=y/SensorManager.GRAVITY\u接地;
float gZ=z/SensorManager.GRAVITY_接地;
//当没有移动时,G力将接近1。
//float gForce=FloatMath.sqrt(gX*gX+gY*gY+gZ*gZ);
float gForce=Math.abs(x+y+z-gX-gY-gZ);
如果(G力>震动\u阈值\u重力){
final long now=System.currentTimeMillis();
//忽略彼此太近的震动事件(500ms)
如果(mShakeTimestamp+震动\u坡度\u时间\u MS>现在){
返回;
}
//3秒无震动后重置震动计数
如果(mShakeTimestamp+震动\u计数\u重置\u时间\u毫秒<现在){
mShakeCount=0;
}
mShakeTimestamp=现在;
mShakeCount++;
姆利斯腾纳·昂沙克(姆沙克账户);
}
}
}
}

我检查了许多链接,但没有得到合适的解决方案。当手机稳定且躺在桌子上未被触碰时,G力值约为8.32。

我遵守了完成震动检测器编码的要求。这是一段用完美的java文件编写的震动检测器代码


编译'com.github.safetysystemtechnology:android震动检测器:v1.2'

。这是一段用完美的java文件编写的震动检测器代码

编译'com.github.safetysystemtechnology:android震动检测器:v1.2'