Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>The worker thread uses Thread.sleep() to perform an efficient delay, wakes up to "do things", sends an update to the UI thread's Handler, and repeats.</p> </blockquote> <p>It is possible this is the the right thing for your app, but since you decided not to describe your actual business problem, it is difficult to tell. For example, if you were writing an email client, and you needed to get control every 15 minutes to check for new email, your proposed solution would be unreliable.</p> <blockquote> <p>The next part is apparently difficult in Android</p> </blockquote> <p>Not especially.</p> <blockquote> <p>The worker thread must have a Looper to create a Handler so it can RECEIVE messages</p> </blockquote> <p>Only if you want to use a <code>Handler</code> for that.</p> <p>Java, as a programming language, has been around for a fairly long time. It has many means of inter-thread communication. <code>Handler</code>, <code>Looper</code>, and <code>HandlerThread</code> wrap around these to offer one particular pattern. That pattern is not suitable for all scenarios, as you have noted. However, the rest of Java still exists, and many standard Java classes are part of Android that you can take advantage of.</p> <p>So, for example, you could use a <code>LinkedBlockingQueue</code>. Have the main application thread put commands onto that queue. Your worker thread, rather than using <code>sleep()</code>, would use <code>poll()</code> with a timeout to either pull an event from the queue or wake up after a period of time.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload