Sunday, April 24, 2011

Creating and Using an Android Service

Services are used in an Android application to perform actions that do not require a user interface. The code called by a Service should run in a separate Thread from the application so it can continue even if the user closes the application. I used a service to download PDF and EPUB files from Connexions. Implementing the service called for creating the Service object, adding it to the ApplicationManifest.xml file, and calling the service from the MenuHandler class.

The Service is created by a class extending IntentService, creating a Constructor that calls the super() constructor and overriding the onHandleIntent() method. The onHandleIntent() method should contain the action to be performed by the Service. In the Connexions app, a DownloadHandler object is created to perform the download in a new Thread.

public class DownloadService extends IntentService
public DownloadService()

protected void onHandleIntent(Intent intent)
DownloadHandler dh = new DownloadHandler();
String url = intent.getStringExtra(DOWNLOAD_URL);
String fileName = intent.getStringExtra(DOWNLOAD_FILE_NAME);
dh.downloadFile(getApplicationContext(), url, fileName);



Adding the Service to the ApplicationManifest.xml is a simple one line addition.

Calling the Service happens in the displayAlert() method in MenuHandler. If the user selects to download the file, the Service is created as an Intent and started.

public void onClick(DialogInterface dialog, int which)
String url = currentContent.getUrl().toString();
Intent intent = new Intent(context, DownloadService.class);

intent.putExtra(DownloadService.DOWNLOAD_URL, MenuUtil.fixPdfURL(currentContent.getUrl().toString(), MenuUtil.getContentType(url)));
intent.putExtra(DownloadService.DOWNLOAD_FILE_NAME, MenuUtil.getTitle(currentContent.getTitle()) + ".pdf");
intent.putExtra(DownloadService.DOWNLOAD_URL, MenuUtil.fixEpubURL(currentContent.getUrl().toString(), MenuUtil.getContentType(url)));
intent.putExtra(DownloadService.DOWNLOAD_FILE_NAME, MenuUtil.getTitle(currentContent.getTitle()) + ".epub");

} });

There are 2 Extras added to the Intent. These are used to pass parameters values that are needed by DownloadHandler. The URL of the file to download and the file name are passed to the Service and are retrieved in the onHandleIntent() method in the service. You can see the code for retrieving them in the onHandleIntent() example above.

As always, you can browse the source or download a zip file of the source. Connexions for Android is available in the Android Market or from the Connexions website.

No comments:

Post a Comment