gpt4 book ai didi

android - 加速度计 : shake the phone detector

转载 作者:行者123 更新时间:2023-11-30 02:27:22 27 4
gpt4 key购买 nike

我一直在使用这个代码 Android: I want to shake it实现摇动运动检测器。但实际上当我在我的智能手机上运行它时,它什么也没做。我尝试使用断点来检查,但它永远不会进入 if (mAccel > 12)。我试着用力摇晃它,但还是认不出来。我认为它需要一些许可,所以我将这一行添加到 list 中:

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

我在 galaxy s3 和 OnePlus One 上试过,我忘记了什么?

EspecificExerciseActivity.java

public class EspecificoExercicio extends Activity {

private ArrayList<Exercicio> listaExercicios;

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

/* do this in onCreate */
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
mSensorManager.registerListener(mSensorListener, mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);
mAccel = 0.00f;
mAccelCurrent = SensorManager.GRAVITY_EARTH;
mAccelLast = SensorManager.GRAVITY_EARTH;

if (mAccel > 12) {
Toast toast1 = Toast.makeText(getApplicationContext(), "Device has shaken.", Toast.LENGTH_LONG);
toast1.show();
}
}

/* put this into your activity class */
private SensorManager mSensorManager;
private float mAccel; // acceleration apart from gravity
private float mAccelCurrent; // current acceleration including gravity
private float mAccelLast; // last acceleration including gravity

private final SensorEventListener mSensorListener = new SensorEventListener() {

public void onSensorChanged(SensorEvent se) {
float x = se.values[0];
float y = se.values[1];
float z = se.values[2];
mAccelLast = mAccelCurrent;
mAccelCurrent = (float) Math.sqrt((double) (x*x + y*y + z*z));
float delta = mAccelCurrent - mAccelLast;
mAccel = mAccel * 0.9f + delta; // perform low-cut filter
}

public void onAccuracyChanged(Sensor sensor, int accuracy) {
if (mAccel > 4) {
Toast toast1 = Toast.makeText(getApplicationContext(), "DevSSDAASSADA", Toast.LENGTH_LONG);
toast1.show();
}
}


};

@Override
protected void onResume() {
super.onResume();
mSensorManager.registerListener(mSensorListener, mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);

}

@Override
protected void onPause() {
mSensorManager.unregisterListener(mSensorListener);
super.onPause();
}

}

最佳答案

您正在检查 onCreate() 中的 if (mAccel > 12)。将检查移动到 onSensorChanged()

关于android - 加速度计 : shake the phone detector,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27765556/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com