从外部方法停止thread.run()

时间:2013-07-09 01:38:58

标签: java android concurrency

我在Android中检测到双指点击与双指滚动。来自运动事件的反馈是订单是:

    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
    1 finger down
    no action for a few milliseconds, which will indicate that there has been a quick 2 finger tap. 

基本上,我希望我的代码执行此操作:如果有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;
    }


}

目前,该线程运行完成,并且没有收到来自MotionEvent的任何通知。线程运行时,MotionEvents不会通过。我应该使用什么样的同步?

编辑:我已完成项目的工作版本。它位于https://github.com/ctuna/MultiTouch

1 个答案:

答案 0 :(得分:1)

使用waitThread.start,而不是.run。