Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If your service is going to be part of you application then you are making it way more complex than it needs to be. Since you have a simple use case of getting some data from a RESTful Web Service, you should look into <a href="http://developer.android.com/reference/android/os/ResultReceiver.html" rel="noreferrer">ResultReceiver</a> and <a href="http://developer.android.com/reference/android/app/IntentService.html" rel="noreferrer">IntentService</a>. </p> <p>This Service + ResultReceiver pattern works by starting or binding to the service with <a href="http://developer.android.com/reference/android/content/Context.html#startService%28android.content.Intent%29" rel="noreferrer">startService()</a> when you want to do some action. You can specify the operation to perform and pass in your ResultReceiver (the activity) through the extras in the Intent.</p> <p>In the service you implement <a href="http://developer.android.com/reference/android/app/IntentService.html#onHandleIntent%28android.content.Intent%29" rel="noreferrer">onHandleIntent</a> to do the operation that is specified in the Intent. When the operation is completed you use the passed in ResultReceiver to <a href="http://developer.android.com/reference/android/os/ResultReceiver.html#send%28int,%20android.os.Bundle%29" rel="noreferrer">send</a> a message back to the Activity at which point <a href="http://developer.android.com/reference/android/os/ResultReceiver.html#onReceiveResult%28int,%20android.os.Bundle%29" rel="noreferrer">onReceiveResult</a> will be called.</p> <p>So for example, you want to pull some data from your Web Service.</p> <ol> <li>You create the intent and call startService.</li> <li>The operation in the service starts and it sends the activity a message saying it started</li> <li>The activity processes the message and shows a progress.</li> <li>The service finishes the operation and sends some data back to your activity.</li> <li>Your activity processes the data and puts in in a list view</li> <li>The service sends you a message saying that it is done, and it kills itself.</li> <li>The activity gets the finish message and hides the progress dialog.</li> </ol> <p>I know you mentioned you didn't want a code base but the open source <a href="http://code.google.com/p/iosched/" rel="noreferrer">Google I/O 2010</a> app uses a service in this way I am describing.</p> <p><strong>Updated to add sample code:</strong></p> <p>The activity.</p> <pre><code>public class HomeActivity extends Activity implements MyResultReceiver.Receiver { public MyResultReceiver mReceiver; public void onCreate(Bundle savedInstanceState) { mReceiver = new MyResultReceiver(new Handler()); mReceiver.setReceiver(this); ... final Intent intent = new Intent(Intent.ACTION_SYNC, null, this, QueryService.class); intent.putExtra("receiver", mReceiver); intent.putExtra("command", "query"); startService(intent); } public void onPause() { mReceiver.setReceiver(null); // clear receiver so no leaks. } public void onReceiveResult(int resultCode, Bundle resultData) { switch (resultCode) { case RUNNING: //show progress break; case FINISHED: List results = resultData.getParcelableList("results"); // do something interesting // hide progress break; case ERROR: // handle the error; break; } } </code></pre> <p>The Service:</p> <pre><code>public class QueryService extends IntentService { protected void onHandleIntent(Intent intent) { final ResultReceiver receiver = intent.getParcelableExtra("receiver"); String command = intent.getStringExtra("command"); Bundle b = new Bundle(); if(command.equals("query") { receiver.send(STATUS_RUNNING, Bundle.EMPTY); try { // get some data or something b.putParcelableArrayList("results", results); receiver.send(STATUS_FINISHED, b) } catch(Exception e) { b.putString(Intent.EXTRA_TEXT, e.toString()); receiver.send(STATUS_ERROR, b); } } } } </code></pre> <p>ResultReceiver extension - edited about to implement MyResultReceiver.Receiver</p> <pre><code>public class MyResultReceiver implements ResultReceiver { private Receiver mReceiver; public MyResultReceiver(Handler handler) { super(handler); } public void setReceiver(Receiver receiver) { mReceiver = receiver; } public interface Receiver { public void onReceiveResult(int resultCode, Bundle resultData); } @Override protected void onReceiveResult(int resultCode, Bundle resultData) { if (mReceiver != null) { mReceiver.onReceiveResult(resultCode, resultData); } } } </code></pre>
 

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