Skip to content Skip to sidebar Skip to footer

Android: Executing The Thread In A Loop With Ontouchlistener()

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 butt

Solution 1:

bLeft.setOnTouchListener(newView.OnTouchListener() {

    @OverridepublicbooleanonTouch(View arg0, MotionEvent event) {
        // TODO Auto-generated method stubMyThreadstart=newMyThread();
         booleanisReleased= event.getAction() == MotionEvent.ACTION_UP || event.getAction() == MotionEvent.ACTION_CANCEL;
         booleanisPressed= event.getAction() == MotionEvent.ACTION_DOWN;

    while (isPressed) {

            start.execute(ip, "left");

            break;
    }
        returnfalse;
    }
});

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.

Solution 2:

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);
 }

Post a Comment for "Android: Executing The Thread In A Loop With Ontouchlistener()"