1 page.title=Sending Work Requests to the Background Service 2 trainingnavtop=true 3 @jd:body 4 <div id="tb-wrapper"> 5 <div id="tb"> 6 <h2>This lesson teaches you to</h2> 7 <ol> 8 <li> 9 <a href="#CreateRequest">Create and Send a Work Request to an IntentService</a> 10 </li> 11 </ol> 12 <h2>You should also read</h2> 13 <ul> 14 <li> 15 <a href="{@docRoot}guide/components/intents-filters.html">Intents and Intent Filters</a> 16 </li> 17 </ul> 18 <h2>Try it out</h2> 19 20 <div class="download-box"> 21 <a href="{@docRoot}shareables/training/ThreadSample.zip" class="button">Download the sample</a> 22 <p class="filename">ThreadSample.zip</p> 23 </div> 24 25 </div> 26 </div> 27 <p> 28 The previous lesson showed you how to create an {@link android.app.IntentService} class. This 29 lesson shows you how to trigger the {@link android.app.IntentService} to run an operation by 30 sending it an {@link android.content.Intent}. This {@link android.content.Intent} can 31 contain optionally contain data for the {@link android.app.IntentService} to process. You can 32 send an {@link android.content.Intent} to an {@link android.app.IntentService} from any point 33 in an {@link android.app.Activity} or {@link android.app.Fragment} 34 </p> 35 <h2 id="CreateRequest">Create and Send a Work Request to an IntentService</h2> 36 <p> 37 To create a work request and send it to an {@link android.app.IntentService}, create an 38 explicit {@link android.content.Intent}, add work request data to it, and send it to 39 {@link android.app.IntentService} by calling 40 {@link android.content.Context#startService startService()}. 41 </p> 42 <p> 43 The next snippets demonstrate this: 44 </p> 45 <ol> 46 <li> 47 Create a new, explicit {@link android.content.Intent} for the 48 {@link android.app.IntentService} called <code>RSSPullService</code>. 49 <br> 50 <pre> 51 /* 52 * Creates a new Intent to start the RSSPullService 53 * IntentService. Passes a URI in the 54 * Intent's "data" field. 55 */ 56 mServiceIntent = new Intent(getActivity(), RSSPullService.class); 57 mServiceIntent.setData(Uri.parse(dataUrl)); 58 </pre> 59 </li> 60 <li> 61 Call {@link android.content.Context#startService startService()} 62 <br> 63 <pre> 64 // Starts the IntentService 65 getActivity().startService(mServiceIntent); 66 </pre> 67 </ol> 68 <p> 69 Notice that you can send the work request from anywhere in an Activity or Fragment. 70 For example, if you need to get user input first, you can send the request from a callback 71 that responds to a button click or similar gesture. 72 </p> 73 <p> 74 Once you call {@link android.content.Context#startService startService()}, 75 the {@link android.app.IntentService} does the work defined in its 76 {@link android.app.IntentService#onHandleIntent onHandleIntent()} method, and then stops itself. 77 </p> 78 <p> 79 The next step is to report the results of the work request back to the originating Activity 80 or Fragment. The next lesson shows you how to do this with a 81 {@link android.content.BroadcastReceiver}. 82 </p> 83