Header Ads

Game developer tip 1: Automatically resume download

This is the first in a series of tips that supplement the Xperia™ game developer recommendations. This first tip expands on recommendation AP13: A download should automatically resume if the connection fails.

The Xperia™ game developer recommendations offers guidance in developing games for Xperia™ devices such as Xperia™ PLAY. The tips in this series provide additional information about some of the significant recommendations in that document. Each tip provides the following information:
Recommendation. This is a brief statement of the recommendation.
Background. This is a fuller description of the recommendation. It answers the questions “why should I follow this recommendation?” and “when or where should I follow this recommendation?”.
Special considerations. This describes any special considerations, such as device-specific considerations or application type-specific considerations that pertain to the recommendation.
Examples. This shows screen captures or code snippets that further illustrate or explain the recommendation.
More information. This links to additional information that relates to the recommendation.
Recommendation
A download should automatically resume if the connection fails.
Background
If a user is trying to download a game, and the device’s connection fails, the download should resume when the connection is restored. This saves the user from having to repeat the download request.
Special considerations
If your download uses the HTTP protocol, you should take advantage of Android’s DownloadManager to automatically retry a download if the connection fails.
The DownloadManager is a system service designed to handle long-running HTTP downloads. It runs the download in the background. If a failure occurs, such as one resulting from a broken connection or a system reboot, the DownloadManager saves the state of the download. After the failure is corrected, for instance, the connection is reestablished, the DownLoadManager attempts to resume the download at the point the download stopped.
In order for the Download Manager to resume the download, the server supplying the download needs to support the HTTP 206 status code and the HTTP ETag response header field. The DownloadManager uses the HTTP 206 status code and the ETag header to manage state for the download. It keeps the ETag and the count of downloaded bytes in a database. This information enables the DownloadManager to determine the point at which to resume the download.
The DownloadManager only supports HTTP downloads. If you use another protocol for the download, such as HTTPS or FTP, you should implement a retry mechanism.
Examples
Here is a code snippet that illustrates how to use the DownLoadManager to manage a download.
//import the Download Manager
import android.app.DownloadManager;
//get instance of the DownloadManager
DownloadManager downloadManager = (DownloadManager)
getSystemService(Context.DOWNLOAD_SERVICE);
//set download URI
Uri downloadUri = Uri.parse(downloadUriString);
//setup download request
DownloadManager.Request downloadRequest = new DownloadManager.Request(downloadUri);
downloadRequest.setDestinationUri(mDestinationUri);
//set allowed network types
downloadRequest.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI);
//set title for the download to appear in the notification area
downloadRequest.setTitle(mFileName);
//set description for the download to appear in the notification area
downloadRequest.setDescription(getText(R.string.downloadDesc).toString());
//initiate download
mDownloadId = downloadManager.enqueue(downloadRequest);

No comments:

Powered by Blogger.