如何检测多次摇晃?

时间:2017-10-26 16:48:54

标签: java android shake

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

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++;
        }

}


}

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

1 个答案:

答案 0 :(得分:0)

如果您打开使用库来检测抖动,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();
  }

}