Java 正在从外部方法停止thread.run()
我在安卓系统中检测到两个手指的点击和两个手指的滚动。运动事件的反馈顺序为:Java 正在从外部方法停止thread.run(),java,android,concurrency,Java,Android,Concurrency,我在安卓系统中检测到两个手指的点击和两个手指的滚动。运动事件的反馈顺序为: 2 fingers down (repeated many times) 1 finger down (repeated a few times) back to 2 fingers down (this indicates a longer hold than a tap. In my case we'll call this scrolling) 或 基本上,我希望我的代码能够做到这一点
2 fingers down (repeated many times)
1 finger down (repeated a few times)
back to 2 fingers down (this indicates a longer hold than a tap. In my case we'll call this scrolling)
或
基本上,我希望我的代码能够做到这一点:如果有1根手指向下,而之前有2根手指向下,请等待几毫秒,看看是否有另一根手指返回,或者是否什么也没有发生。如果另一个手指回来,停止等待。如果什么也没发生,就会有水龙头
public boolean onGenericMotionEvent(MotionEvent event) {
int numFingers = event.getPointerCount();
switch (event.getActionIndex()){
case (MotionEvent.ACTION_DOWN):
if (waitThread != null){
stateChange=true;
}
if (numFingers==2){
hasHadTwoDown=true;
}
if (numFingers==1){
//FIRE A THREAD THAT WAITS
if (hasHadTwoDown){
waitThread = new WaitThread();
waitThread.run();
}
}
}
gestureDetector.onTouchEvent(event);
}
线呢
private class WaitThread extends Thread {
public WaitThread(){
stateChange=false;
}
@Override
public void run(){
Log.i("myGesture", "thread is running");
long startTime = Calendar.getInstance().getTimeInMillis();
while(!stateChange && Calendar.getInstance().getTimeInMillis() - startTime < 100){
//wait until getting a notification that state changes or timeout
}
if (stateChange){
waitThread=null;
//no double click
return;
}
//double click
Log.i("myGesture", "double click");
hasHadTwoDown=false;
waitThread=null;
}
}
私有类WaitThread扩展线程{
公共WaitThread(){
stateChange=false;
}
@凌驾
公开募捐{
Log.i(“我的手势”,“线程正在运行”);
long startTime=Calendar.getInstance().getTimeInMillis();
而(!stateChange&&Calendar.getInstance().getTimeInMillis()-startTime<100){
//等待,直到收到状态更改或超时的通知
}
如果(状态更改){
waitThread=null;
//没有双击
返回;
}
//双击
Log.i(“我的手势”,“双击”);
hashadtown=false;
waitThread=null;
}
}
当前,线程运行到完成,并且没有收到来自MotionEvent的任何通知。当线程运行时,MotionEvents不会执行。我应该使用哪种同步
编辑:我已完成项目的工作版本。可以在使用waitThread.start,而不是.run上找到它。我会有一个计数器变量,用于计算线程中的毫秒数。然后在onGenericMotionEvent方法中处理抽头。例如,如果numFingers==2&&timeVariable>something,那么就做些什么。。。
private class WaitThread extends Thread {
public WaitThread(){
stateChange=false;
}
@Override
public void run(){
Log.i("myGesture", "thread is running");
long startTime = Calendar.getInstance().getTimeInMillis();
while(!stateChange && Calendar.getInstance().getTimeInMillis() - startTime < 100){
//wait until getting a notification that state changes or timeout
}
if (stateChange){
waitThread=null;
//no double click
return;
}
//double click
Log.i("myGesture", "double click");
hasHadTwoDown=false;
waitThread=null;
}
}