-1

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 button the String must be written in a loop. Here is what i am trying to do.

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

Somehow i don't know why it is not working. start,execute(string, string) executes the thread in AsyncTask class to create a socket and output stream.

user1422066
  • 29
  • 1
  • 7

2 Answers2

0
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.

QVDev
  • 1,093
  • 10
  • 17
  • actually it is executing but the app crashes after sending the string to the server which means it is executing but the app crashes after pressing the button and btw i am using asynctask – user1422066 Jun 05 '12 at 18:44
  • So what are you exactly sending to the server? You have any code? Or a stack trace for that matter? – QVDev Jun 06 '12 at 07:20
  • i am sending strings to server on button events i want if the user press and holds the button the string should be sent to the server continuously according to the time presses down. – user1422066 Jun 06 '12 at 08:43
  • You have a stack trace of the crash? it seems the string you are trying to sent is null, it is hard to see without more information – QVDev Jun 06 '12 at 09:16
  • hey what if i use service instead of asynctask to run the socket in background? how can i do that with my above code? – user1422066 Jun 16 '12 at 14:32
  • For more information about service see this thread: http://stackoverflow.com/questions/11255438/online-transaction-asynctask-vs-service – QVDev Jul 11 '12 at 07:56
0

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);
 }
me_
  • 681
  • 1
  • 8
  • 18