Creating an IntentService
Because services, like activities, respond to intents, they must also be declared in your
AndroidManifest.xml. Add an element for PollService to your manifest.
Listing 28.2 Adding service to manifest (AndroidManifest.xml)
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.bignerdranch.android.photogallery" >
<application
... >
<activity
android:name=".PhotoGalleryActivity"
android:label="@string/app_name" >
...
Then add code to start your service inside PhotoGalleryFragment.
Listing 28.3 Adding service startup code (PhotoGalleryFragment.java)
public class PhotoGalleryFragment extends Fragment {
private static final String TAG = "PhotoGalleryFragment";
...
@Override
public void onCreate(Bundle savedInstanceState) {
...
updateItems();
Intent i = PollService.newIntent(getActivity());
getActivity().startService(i);
Handler responseHandler = new Handler();
mThumbnailDownloader = new ThumbnailDownloader<>(responseHandler);
...
}
...
}
Fire this up and see what you get. You should see something like this in Logcat:
02-23 14:25:32.450 2692-2717/com.bignerdranch.android.photogallery I/PollService:
Received an intent: Intent { cmp=com.bignerdranch.android.photogallery/.PollService }