The DownloadManager sample application demonstrates how you can asynchronously download the content of an URL to the device storage. For information on creating the sample application project in the IDE, see Creating Sample Applications, and on the coding of the sample application, see Download Manager task. The following figure illustrates the main screens of the DownloadManager. Figure: DownloadManager screens
The application opens with the DownloadManager main screen, where the download history is displayed. To download a file, type its URL address in the input field at the top of the screen and click the OK button to initiate the process. The new download status item is added with a progress bar and buttons that allow pausing or canceling the process. When the specified content is downloaded, click the Open button to run an external application to view the content, or click the Remove button to remove the item from the download history. To clear the whole download history, click the Clear all finished button. Source FilesYou can create and view the sample application project including the source files in the IDE.
ImplementationThe download process starts when the user clicks OK next to the URL input field on the main screen. The button click indirectly invokes the startDownloading() method, which is responsible for starting the download process. A new DownloadRequest object is created and the download process is started through the DownloadManager object. /* js/app.model.js */ startDownloading: function Model_startDownloading(url, eventHandlers, onSuccess) { var downloadRequest, downloadId; try { downloadRequest = new tizen.DownloadRequest(url, 'downloads'); downloadId = tizen.download.start(downloadRequest, this.createDownloadCallback(eventHandlers)); this.onDownloadStarted(onSuccess, downloadId, url); } catch (error) { console.error('Start download error', error); } }, After the download process is started successfully, the download item information is added to the local storage and the application user interface is updated. The startDownloading() method receives the onDownloadStarted() method as a callback. /* js/app.js */ onDownloadStarted: function App_onDownloadStarted(id, url) { var fileName = this.helpers.getSourceName(url); this.ui.onDownloadStarted(id, url, fileName); }, To pause downloading, the application uses the pauseDownloading() method. /* app.model.js */ pauseDownloading: function Model_pauseDownloading(downloadId) { try { tizen.download.pause(downloadId); } catch (error) { console.error('Pause download error', error); } }, To resume downloading, the application uses the resumeDownloading() method. /* app.model.js */ resumeDownloading: function Model_resumeDownloading(downloadId, eventHandlers, listenerCallback) { tizen.download.resume(downloadId); if (eventHandlers !== null) { tizen.download.setListener(downloadId, this.createDownloadCallback(eventHandlers)); listenerCallback(downloadId); } }, To cancel downloading, the application uses the cancelDownloading() method. /* app.model.js */ cancelDownloading: function Model_cancelDownloading(downloadId) { try { tizen.download.cancel(downloadId); } catch (error) { console.error('Cancel download error', error); } }, To launch a specified file, the application uses the launchFile() method. /* app.model.js */ launchFile: function Model_launchFile(fileName, mime) { var service; service = new tizen.ApplicationControl('http:///appcontrol/operation/view', 'file:///opt/usr/media/Downloads/' + fileName, mime, null); try { tizen.application.launchAppControl(service, null, function onLaunchFileSuccess() { console.log('Launch file success'); }, function onLaunchFileError(error) { console.error('Launch file launchAppControl error: ', error); if (error.type === 'NotFoundError') { alert('There is no suitable application ' + 'to show this content. ' + 'Please install application ' + 'that supports this content.'); } }); } catch (error) { console.error('Launch file error', error.message); } }, |
|
来自: gearss > 《 Tizen开发》