0

I am developing an App. It has multiple button , each button will send the TCP data to the Server.

For example: There has three Button for Button-A , Button-B and Button-C. The Button-A will send the "A" to the Server , etc.

private static String data;

btn_A.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                // TODO Auto-generated method stub

                data = "A";
                send(data);
            }
        });

btn_B.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                // TODO Auto-generated method stub

                data = "B";
                send(data);
            }
        });

btn_C.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                // TODO Auto-generated method stub

                data = "C";
                send(data);
            }
        });

If I click the Button from Button-A to Button-C quickly. The Server will receive data for three times. It is inefficient...

How to implement click multiple button but only working for last time click ?

I want the App only send the data for the last button which I have click if I click the Button from Button-A to Button-C quickly.

I have try to use the thread for sending data to Server every 3 second. It will send the last value of data every 3 second.

Does there has other better way to do this ?

Martin
  • 2,813
  • 11
  • 43
  • 66

1 Answers1

0

Well, as I saw in the comments somebody told you will be able to solve it by AsyncTask. This would be my recommendation too... You will have to set up a Handler and a method to cancel() the task.

Look at these two links: AsyncTask by Vogella and how to cancel task here on SO

Hope it helps.

Community
  • 1
  • 1
Martin Pfeffer
  • 12,471
  • 9
  • 59
  • 68