There is a code that in the stream receives data from the controller:

Handler mHandler = new Handler(); private void tvAppend(TextView tv, CharSequence text) { final TextView ftv = tv; final CharSequence ftext = text; mHandler.post(new Runnable() { @Override public void run() { ftv.append(ftext); } }); } 

How can I stuff a team in this thread that should work forever in a stream with a delay of 100 milliseconds

 bytesToSend = addCRC(new byte[]{1, 0x3, 0, 0 ,0, 0}); mPhysicaloid.write(bytesToSend, bytesToSend.length); 

All code can be seen https://github.com/ksksue/PhysicaloidLibrary/blob/master/SampleProjects/tutorial5/src/com/physicaloid/tutorial5/Tutorial5Activity.java


UPD: If I correctly understood the answer @ saidolim-djuraev, then my code now should be like this:

 Handler mHandler = new Handler(); private void tvAppend(TextView tv, CharSequence text) { final TextView ftv = tv; final CharSequence ftext = text; mHandler.post(new Runnable() { @Override public void run() { ftv.append(ftext); while (true) { bytesToSend = addCRC(new byte[]{1, 0x3, 0, 0, 0, 0}); mPhysicaloid.write(bytesToSend, bytesToSend.length); try { Thread.sleep(100); } catch (Exception ex) { } } } }); } 

But how then to start this thread in one method, and stop the flow when executing another method?

  • Но как тогда этот поток запустить в одном методе, а остановить поток при выполнении другого метода ? this is another question - Saidolim

2 answers 2

There are a couple of moments:

  1. Handler is used to execute code in a UI thread, and you should not do any other things in it.
  2. In order to do some kind of operation with a fixed delay or a fixed frequency, you should use the java.util.concurrent package.

     ScheduledExecutorService mExecutor = Executors.newSingleThreadScheduledExecutor(); private void tvAppend(TextView tv, CharSequence text) { final TextView ftv = tv; final CharSequence ftext = text; ftv.append(ftext); mExecutor.scheduleAtFixedRate(getRunnable(addCRC(new byte[]{1, 0x3, 0, 0 ,0, 0})), 0, 100 , TimeUnit.MILLISECONDS); } public Runnable getRunnable(final byte[] data){ return new Runnable(){ @Override public void run(){ mPhysicaloid.write(data, data.length); } }; } 
  • If I understand correctly, do you suggest using your method instead of my Handler? - Andrey
  • Yes. If the Handler executes a slow code, and especially sleep in it, the application UI will simply hang and will not respond. Plus, it seems in a similar way (the same mExecutor, just another method) I suggested a solution to another question of yours, so the code will have some integrity and order. - Mentat
  • I recommend to get acquainted: habrahabr.ru/post/116363 - Mentat
  • See: link 1 and link 2 How can I fix this? - Andrey
  • Sori, typo. new Runnable () {...} - Mentat

try

 @Override public void run() { ftv.append(ftext); while(true){ bytesToSend = addCRC(new byte[]{1, 0x3, 0, 0 ,0, 0}); mPhysicaloid.write(bytesToSend, bytesToSend.length); try { Thread.sleep(100); } catch (Exception ex) { } } } 

UPD:

for

But how then to start this thread in one method, and stop the flow when executing another method?

you need to create a separate class for the runnable and create a boolean variable to control the while . as:

 private static boolean canWork=true; ... while(canWork) {...} 

for stop doing

 <имя класса>.canWork = false; 
  • I will try this method a little later, but I immediately wondered if it was possible to stop the flow, if so how? And another stupid question is whether it is possible to pause and wait for an answer when sending another command in a given cycle, and as soon as the answer came, the cycle would continue. Answers are received by append (True if I understand everything correctly). (Do not judge strictly, did not work with the streams, and I am new to this) - Andrei