簡體   English   中英

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

[英]Stopping thread.run() from an outside method

我在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獲得

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM