【问题标题】:detect Shake in android检测Android中的Shake
【发布时间】:2016-07-28 10:36:59
【问题描述】:

当用户摇动设备 10 次时,我正在尝试访问 API。我尝试了许多 git 示例和堆栈溢出解决方案,但没有一个解决了我的问题。其中一些在 10 次之前或 10 次之后检测到抖动。我试过SeiamicShakeDetector 库。请给我一些有价值的解决方案。

【问题讨论】:

  • 连续多次摇晃没那么简单。我相信您需要自己定义 shake 是什么以及两次摇动之间的 timeout 是什么,以便对解决方案感到满意

标签: android android-sensors shake


【解决方案1】:

我已经使用这个库做到了:

1) 在 build.gridle 文件中添加依赖

allprojects {
  repositories {
    ...
    maven { url 'https://jitpack.io' }
  }
}

dependencies {
   compile 'com.github.safetysystemtechnology:android-shake-detector:v1.2'
}

2) 授予您应用清单文件的权限

<uses-feature android:name="android.hardware.sensor.accelerometer" android:required="true" />

如果您将在后台运行,请注册您的广播接收器

<receiver android:name=".ShakeReceiver">
    <intent-filter>
        <action android:name="shake.detector" />
    </intent-filter>
</receiver>

3) 在 onCreate 方法中启动它 像这样:

private ShakeDetector shakeDetector;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    buildView();

    ShakeOptions options = new ShakeOptions()
            .background(true)
            .interval(1000)
            .shakeCount(2)
            .sensibility(2.0f);

    this.shakeDetector = new ShakeDetector(options).start(this, new ShakeCallback() {
        @Override
        public void onShake() {
            Log.d("event", "onShake");
        }
    });

    //IF YOU WANT JUST IN BACKGROUND
    //this.shakeDetector = new ShakeDetector(options).start(this);
}

4) 覆盖 onStop 方法并停止该方法

@Override
protected void onStop() {
    super.onStop();
shakeDetector.stopShakeDetector(getBaseContext());
}

5) 重写 onDistroy 方法并像这样进行分发:

@Override
protected void onDestroy() {
    shakeDetector.destroy(getBaseContext());
    super.onDestroy();
}

(*) 可选步骤:如果您将在后台运行,请创建广播接收器

public class ShakeReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (null != intent && intent.getAction().equals("shake.detector")) {
            ...
        }
    }
}

【讨论】:

    【解决方案2】:

    通过使用 SensorListener

    请检查这个勾选的答案...

    您必须调整 SHAKE_THRESHOLD 值才能达到此目的

    How to detect shake event with android?

    谢谢!

    【讨论】:

      【解决方案3】:

      有一个静态变量,每次检测到抖动时都会递增。

      static int count = 0;
      public void onSensorChanged(SensorEvent event) {
      if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
          long curTime = System.currentTimeMillis();
          if ((curTime - mLastShakeTime) > MIN_TIME_BETWEEN_SHAKES_MILLISECS) {
      
              float x = event.values[0];
              float y = event.values[1];
              float z = event.values[2];
      
              double acceleration = Math.sqrt(Math.pow(x, 2) +
                      Math.pow(y, 2) +
                      Math.pow(z, 2)) - SensorManager.GRAVITY_EARTH;
      
              if (acceleration > SHAKE_THRESHOLD) {
                  mLastShakeTime = curTime;
                  count++;
                  if(count==10){
                      //your code goes here
                  }
              }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-30
        • 1970-01-01
        • 2014-04-06
        • 2011-05-06
        • 2011-02-04
        相关资源
        最近更新 更多