如何检测多次震动?

How to detect multiple shakes?

我正在尝试制作一个应用程序,它会根据用户摇动设备的次数做出不同的响应。具体来说,相机和浏览器。这是我的代码:

package com.example.vikin.servicetest;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.IBinder;
import android.widget.Toast;

import java.util.Timer;
import java.util.TimerTask;

public class BackgoundService extends Service implements SensorEventListener {
private SensorManager senSensorManager;
private Sensor senAccelerometer;
private long lastUpdate = 0;
private float last_x, last_y, last_z;
private static final int SHAKE_THRESHOLD = 600;
float speed;
float distance;
int shakeCount = 0;
Timer timer = new Timer(true);

public BackgoundService() {
}

@Override
public void onSensorChanged(SensorEvent sensorEvent) {
    Sensor mySensor = sensorEvent.sensor;

    if (mySensor.getType() == Sensor.TYPE_ACCELEROMETER) {
        float x = sensorEvent.values[0];
        float y = sensorEvent.values[1];
        float z = sensorEvent.values[2];

        long curTime = System.currentTimeMillis();
        if ((curTime - lastUpdate) > 10) {
            long diffTime = (curTime - lastUpdate);
            lastUpdate = curTime;

            speed = Math.abs(x + y + z - last_x - last_y - last_z) / diffTime * 10000;
            distance = Math.abs(x + y + z - last_x - last_y - last_z);
            if (speed > SHAKE_THRESHOLD) {
                //Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
                //Intent intent1 = getPackageManager().getLaunchIntentForPackage("com.android.chrome");
                //startActivity(intent);
                for(long i = 0; i < 20; i++){
                    try {
                        Thread.sleep(50);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    if(speed > SHAKE_THRESHOLD) {
                        shakeCount++;
                    }
                }
            }

            last_x = x;
            last_y = y;
            last_z = z;
        }
    }
}

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

}

@Override
public void onCreate() {
    Toast.makeText(this, "Service Started.", Toast.LENGTH_SHORT).show();
    senSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    senAccelerometer = senSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
    senSensorManager.registerListener(this, senAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
}

@Override
public IBinder onBind(Intent intent) {

    return null;
}

@Override
public void onDestroy() {

    Toast.makeText(this, "Service Terminated.", Toast.LENGTH_SHORT).show();
}

public void countShakes() {
    if(shakeCount == 0) {
        timer.schedule(new TimerTask() {

            @Override
            public void run() {
                switch (shakeCount) {
                    case 2:
                        Intent intent1 = getPackageManager().getLaunchIntentForPackage("com.android.chrome");
                        startActivity(intent1);
                        break;
                    case 1:
                        Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
                        startActivity(intent);
                        break;
                    default:

                        break;
                }
            }
        }, 1000);
    }
        if (speed < 100) {
            shakeCount++;
        }

}


}

当我在我的设备上测试它时,它要么无法打开任何东西,要么会打开错误的应用程序。有人有什么建议吗?

如果您愿意使用库来检测抖动,https://github.com/square/seismic 真的很容易使用并且效果很好。这是他们的示例应用程序中的示例。您只需启动振动检测器,然后在 hearShake() 方法中执行一些操作。

package com.squareup.seismic.sample;

import android.app.Activity;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
import com.squareup.seismic.ShakeDetector;
import static android.view.Gravity.CENTER;
import static android.view.ViewGroup.LayoutParams;
import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;

public class Demo extends Activity implements ShakeDetector.Listener {
  @Override protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    SensorManager sensorManager = (SensorManager) 
    getSystemService(SENSOR_SERVICE);
    ShakeDetector sd = new ShakeDetector(this);
    sd.start(sensorManager);

    TextView tv = new TextView(this);
    tv.setGravity(CENTER);
    tv.setText("Shake me, bro!");
    setContentView(tv, new LayoutParams(MATCH_PARENT, MATCH_PARENT));
  }

  @Override public void hearShake() {
    Toast.makeText(this, "Don't shake me, bro!", Toast.LENGTH_SHORT).show();
  }

}