There is a client-server system. Client on android server on Delphi. Everything basically works, click on the button, the message is read from edit and sent. The question is how to make the message sent not once, but constantly, until I release the key. Here is the code to send the message:

public void onClick3(View view) { try { EditText et = (EditText) findViewById(R.id.editText3); String str = et.getText().toString(); PrintWriter out = new PrintWriter(new BufferedWriter( new OutputStreamWriter(socket.getOutputStream())), true); out.println(str); } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } 

    3 answers 3

     yourButton.setOnTouchListener(new View.OnTouchListener() {) @Override public boolean onTouch(View view, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: // отправляем сообщения, снова и снова break; case MotionEvent.ACTION_UP: // прекращаем отправку сообщений break; } return true; } }); 
    • something does not work for me, it crashes, although I probably just do not write correctly due to inexperience. - Stenlav
    • Show the arising exception. - falstaf
    • For some reason it seems to me that the ACTION_DOWN and ACTION_UP events work once. And, the comment, "// send messages, again and again," should be considered as "// We pop the flag to send messages". And, in a separate thread, we start in the cycle the sending of messages, if this flag is set. - Chad
    • I am a beginner, in this matter, I am doing a task, according to my studies, I can’t do it in any way. If I send you all the sources of what is already working, you will be able to implement on one button, for example, this system of sending messages so that I figured out later? - Stenlav
    • @Stenlav, you better try to figure it out yourself, and if you have any difficulties with something specific, ask questions. - falstaf

    It is necessary to catch both events of pressing and releasing.

    1. On event ACTION_DOWN start asynchronous loop (thread) sending messages
    2. Then, by ACTION_UP stop the message sending cycle, by stopping the thread of the sending message.

      And if you send a message to the MouseDown event?

      • where, forgive, is such an event in Android ??? - Vladyslav Matviienko
      • Generally have yourself completely calm. And the mouse can be connected without steaming, at the kernel level. Take the adapter from micro usb to mother, plug a mouse in there and enjoy it on your phone :-) - Chad
      • @ Chad, what are you saying ?? Otherwise, I do not know what OTG is. But the MouseDown event, no matter how hard you try, you will not trigger it in any way. (You can, of course, rewrite from Android sources, add such an event, but it will not start working on all Android devices from this) - Vladyslav Matviienko
      • @metalurgus, really yours :-) I meant that the event from the mouse separately can be caught quite calmly. But it is not rendered separately, and can be recognized as a clarification. What is OTG? :-) - Chad