Android: executing the thread in a loop with onTouchListener()

后端 未结 2 936
你的背包
你的背包 2021-01-29 07:57

Hi in my app there are 8 buttons. Each button is configured onclickListener() when it is clicked the String is written ti the socket. Now i want that when i press and hold the b

相关标签:
2条回答
  • 2021-01-29 08:19

    an asynchronos task can only execute one time... you are trying to employ it in a while loop... meaning you are trying to use it repeatedly... you need to re-initialize start for each iteration of the while loop...

     while (ispressed){
          start = new YourAsyncTask();
          start.execute(params);
     }
    
    0 讨论(0)
  • 2021-01-29 08:22
    bLeft.setOnTouchListener(new View.OnTouchListener() {
    
        @Override
        public boolean onTouch(View arg0, MotionEvent event) {
            // TODO Auto-generated method stub
            MyThread start = new MyThread();
             boolean isReleased = event.getAction() == MotionEvent.ACTION_UP || event.getAction() == MotionEvent.ACTION_CANCEL;
             boolean isPressed = event.getAction() == MotionEvent.ACTION_DOWN;
    
        while (isPressed) {
    
                start.execute(ip, "left");
    
                break;
        }
            return false;
        }
    });
    

    First of all, isReleased wont be used so get rid of that variable this makes things more complicated.

    Seconds thing make sure it ends up in the while loop for example:

    `Log.d("debugText", "I am in the while")'

    and also log the press state. This will make things more clear while debugging.

    Also take a look into the following documentation of Android about painless Threading. Make sure you are using the right stuff.

    After debugging you probably will understand why it is not executing. If not, more information is needed.

    0 讨论(0)
提交回复
热议问题