Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>How can I set up my Activity to be listening to the Service? Is this the best way to approach this problem?</p> </blockquote> <p>You have three major options, as I see it:</p> <ol> <li><p>Polling. The <code>Activity</code> periodically asks the <code>Service</code> for the latest data. IMHO, this option sucks, but it's certainly possible.</p></li> <li><p>Callbacks. Per jax's answer, the <code>Activity</code> registers a callback object ("observer") with the <code>Service</code>. The <code>Service</code> invokes a method on the callback when the data changes, which in turn updates the UI. You can see an example of using that with a <code>Service</code> <a href="http://github.com/commonsguy/cw-andtutorials/tree/master/18-LocalService/" rel="noreferrer">here</a>.</p></li> <li><p>Broadcast <code>Intents</code>. The <code>Service</code> broadcasts an <code>Intent</code> via <code>sendBroadcast()</code> on a data change. The <code>Activity</code> registers a <code>BroadcastReceiver</code> using <code>registerReceiver()</code>, and that <code>BroadcastReceiver</code> is notified of an incoming broadcast. This triggers the <code>Activity</code> to load the latest data from the <code>Service</code>, or possibly just to get the latest data out of extras in the broadcast <code>Intent</code>. You can see an example of using that technique with a <code>Service</code> <a href="https://github.com/commonsguy/cw-omnibus/tree/master/Internet/Download" rel="noreferrer">here</a>.</p></li> </ol>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. 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