Guest User

Untitled

a guest
Mar 22nd, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.18 KB | None | 0 0
  1. package com.ingeniouscar.utils;
  2.  
  3.  
  4. import android.app.DownloadManager;
  5. import android.content.BroadcastReceiver;
  6. import android.content.Context;
  7. import android.content.Intent;
  8. import android.content.IntentFilter;
  9. import android.net.Uri;
  10. import android.os.Environment;
  11. import android.widget.Toast;
  12.  
  13. import static android.content.Context.DOWNLOAD_SERVICE;
  14.  
  15.  
  16.  
  17. public class DownloadHelper {
  18.  
  19. private DownloadManager downloadManager;
  20. private long downloadReference;
  21.  
  22.  
  23. public DownloadHelper(Context context,String url) {
  24. context.registerReceiver(onComplete,
  25. new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
  26. context.registerReceiver(onNotificationClick,
  27. new IntentFilter(DownloadManager.ACTION_NOTIFICATION_CLICKED));
  28. downloadManager = (DownloadManager) context.getSystemService(DOWNLOAD_SERVICE);
  29. Uri Download_Uri = Uri.parse(url);
  30. DownloadManager.Request request = new DownloadManager.Request(Download_Uri);
  31.  
  32. //Restrict the types of networks over which this download may proceed.
  33. request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE);
  34.  
  35. //Set whether this download may proceed over a roaming connection.
  36. request.setAllowedOverRoaming(true);
  37. //Set the title of this download, to be displayed in notifications (if enabled).
  38. request.setTitle("Way Point Image Download");
  39. String fileName = url.substring(url.lastIndexOf('/') + 1);
  40.  
  41. request.setDestinationInExternalPublicDir( Environment.DIRECTORY_DOWNLOADS,fileName);
  42. request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
  43. //Enqueue a new download and same the referenceId
  44. downloadReference = downloadManager.enqueue(request);
  45.  
  46. }
  47.  
  48.  
  49. BroadcastReceiver onComplete=new BroadcastReceiver() {
  50. public void onReceive(Context ctxt, Intent intent) {
  51. Toast.makeText(ctxt, "download complete!", Toast.LENGTH_LONG).show();
  52. }
  53. };
  54.  
  55. BroadcastReceiver onNotificationClick=new BroadcastReceiver() {
  56. public void onReceive(Context ctxt, Intent intent) {
  57.  
  58. }
  59. };
  60.  
  61.  
  62. }
Add Comment
Please, Sign In to add comment