Guest User

Untitled

a guest
Nov 20th, 2017
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.24 KB | None | 0 0
  1. ## Using PRDownloader Library in your Android application
  2.  
  3. Add this in your build.gradle
  4. ```groovy
  5. compile 'com.mindorks.android:prdownloader:0.2.0'
  6. ```
  7. Do not forget to add internet permission in manifest if already not present
  8. ```xml
  9. <uses-permission android:name="android.permission.INTERNET" />
  10. ```
  11. Then initialize it in onCreate() Method of application class :
  12. ```java
  13. PRDownloader.initialize(getApplicationContext());
  14. ```
  15. Initializing it with some customization
  16. ```java
  17. // Enabling database for resume support even after the application is killed:
  18. PRDownloaderConfig config = PRDownloaderConfig.newBuilder()
  19. .setDatabaseEnabled(true)
  20. .build();
  21. PRDownloader.initialize(getApplicationContext(), config);
  22.  
  23. // Setting timeout globally for the download network requests:
  24. PRDownloaderConfig config = PRDownloaderConfig.newBuilder()
  25. .setReadTimeout(30_000)
  26. .setConnectTimeout(30_000)
  27. .build();
  28. PRDownloader.initialize(getApplicationContext(), config);
  29. ```
  30.  
  31. ### Make a download request
  32. ```java
  33. int downloadId = PRDownloader.download(url, dirPath, fileName)
  34. .build()
  35. .setOnStartOrResumeListener(new OnStartOrResumeListener() {
  36. @Override
  37. public void onStartOrResume() {
  38.  
  39. }
  40. })
  41. .setOnPauseListener(new OnPauseListener() {
  42. @Override
  43. public void onPause() {
  44.  
  45. }
  46. })
  47. .setOnCancelListener(new OnCancelListener() {
  48. @Override
  49. public void onCancel() {
  50.  
  51. }
  52. })
  53. .setOnProgressListener(new OnProgressListener() {
  54. @Override
  55. public void onProgress(Progress progress) {
  56.  
  57. }
  58. })
  59. .start(new OnDownloadListener() {
  60. @Override
  61. public void onDownloadComplete() {
  62.  
  63. }
  64.  
  65. @Override
  66. public void onError(Error error) {
  67.  
  68. }
  69. });
  70. ```
  71.  
  72. ### Pause a download request
  73. ```java
  74. PRDownloader.pause(downloadId);
  75. ```
  76.  
  77. ### Resume a download request
  78. ```java
  79. PRDownloader.resume(downloadId);
  80. ```
  81.  
  82. ### Cancel a download request
  83. ```java
  84. // Cancel with the download id
  85. PRDownloader.cancel(downloadId);
  86. // The tag can be set to any request and then can be used to cancel the request
  87. PRDownloader.cancel(TAG);
  88. // Cancel all the requests
  89. PRDownloader.cancelAll();
  90. ```
  91.  
  92. ### Status of a download request
  93. ```java
  94. Status status = PRDownloader.getStatus(downloadId);
  95. ```
  96.  
  97. ### Clean up resumed files if database enabled
  98. ```java
  99. // Method to clean up temporary resumed files which is older than the given day
  100. PRDownloader.cleanUp(days);
  101. ```
Add Comment
Please, Sign In to add comment