我正在创建一个android应用程序,我正在添加一个使用库seismichttps://github.com/square/seismic
有了这个摇动功能,我正试图使它,以便根据每一个摇动的计数,它将打开一个相应的页面,即摇动一次为第1页,摇动两次为第2页,以此类推。
我正在使用一个处理程序在启动if
语句之前等待1700ms,以便它知道正确的计数,而不是直接转到第一个if语句(当count为1时),但它仍然从第1页转到第2页再转到第3页,而不是等待并在第一次转到正确的页。
本垒打
mShakeDetector.setOnShakeListener(new com.example.name.project.ShakeDetector.OnShakeListener() {
@Override
public void onShake(final int count) {
System.out.println(count);
final Intent tts = new Intent(context, ttsScreen.class);
final Intent stt = new Intent(context, sttScreen.class);
final Intent cbb = new Intent(context, cbbScreen.class);
final Intent ocr = new Intent(context, ocrScreen.class);
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
if( count == 1 ) {
startActivity(tts);
}
else if (count == 2) {
startActivity(stt);
}
else if (count == 3) {
startActivity(cbb);
}
else if (count == 4) {
startActivity(ocr);
}
}
}, 1800);
}
});
振动检测器等级
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.util.FloatMath;
public class ShakeDetector implements SensorEventListener {
/*
* 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 = (float)Math.sqrt( gX * gX + gY * gY + gZ * 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);
}
}
}
}
所以基本上,shake detector类在重置之前注册3秒的shake counts,这很好,但现在在我的home类中,我希望它注册我的shake,等待x秒,然后正确地打开类,但我不确定我的方法有什么问题?
我加了一个系统,它的探测效果很好
I/System.out: 1I/System.out: 2I/System.out: 3
但是从第1页到第2页再到第3页(您可以看到转换);这是什么原因?
最佳答案
onShake
每次震动都会多次呼叫。
这里的第一个问题是每次都发布一个Runnable
,但是调用了onShake()
,但是没有取消最后一个问题。因此,在发布新消息之前,您需要取消队列中最后一个Runnable
。
试试下面的代码:
public class MainActivity extends AppCompatActivity {
private int shakeCount=0;
private Runnable runnable ;
private Handler handler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
runnable =new Runnable() {
public void run() {
if( shakeCount == 1 ) {
final Intent tts = new Intent(context, ttsScreen.class);
startActivity(tts);
}
else if (shakeCount == 2) {
final Intent stt = new Intent(context, sttScreen.class);
startActivity(stt);
}
else if (shakeCount == 3) {
final Intent cbb = new Intent(context, cbbScreen.class);
startActivity(cbb);
}
else if (shakeCount == 4) {
final Intent ocr = new Intent(context, ocrScreen.class);
startActivity(ocr);
}
shakeCount=0;
}
};
mShakeDetector.setOnShakeListener(new com.example.name.project.ShakeDetector.OnShakeListener() {
@Override
public void onShake(final int count) {
shakeCount=count;
handler.removeCallbacks(runnable);
handler.postDelayed(runnable, 1800);
});
}
我在那里放了一个activity类,只是为了区分局部变量和全局变量。
关于java - 使用可运行处理程序的if语句,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54905649/