Advertisement
Guest User

Untitled

a guest
Jun 21st, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.55 KB | None | 0 0
  1. package com.android.jsondownload;
  2.  
  3. import android.app.DownloadManager;
  4. import android.app.NotificationManager;
  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.support.v4.app.NotificationCompat;
  12. import android.widget.Button;
  13.  
  14. import java.util.ArrayList;
  15.  
  16. public class Downloader {
  17.  
  18.     private DownloadManager downloader;
  19.  
  20.     ArrayList<Long> list = new ArrayList<>();
  21.     private long refid;
  22.     private Button mbtnStartDownload;
  23.  
  24.     private Context mContext;
  25.  
  26.     public Downloader(Context context) {
  27.         this.mContext = context;
  28.     }
  29.  
  30.     public void startDownload(String url, int i){
  31.  
  32.         downloader = (DownloadManager) mContext.getSystemService(Context.DOWNLOAD_SERVICE);
  33.         mContext.registerReceiver(onComplete, new IntentFilter(android.app.DownloadManager.ACTION_DOWNLOAD_COMPLETE));
  34.  
  35.         android.app.DownloadManager.Request request = new android.app.DownloadManager.Request(Uri.parse(url));
  36.         request.setAllowedNetworkTypes(android.app.DownloadManager.Request.NETWORK_WIFI | android.app.DownloadManager.Request.NETWORK_MOBILE);
  37.         request.setAllowedOverRoaming(false);
  38.         request.setTitle("Downloading " + "Halfdata_" + i + ".zip");
  39.         request.setDescription("Halfdata_" + i + ".zip");
  40.         request.setVisibleInDownloadsUi(true);
  41.         request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "/halfdatas/" + "/" + "halfdata_" + i + ".zip");
  42.  
  43.         refid = downloader.enqueue(request);
  44.         list.add(refid);
  45.  
  46.     }
  47.  
  48.     private BroadcastReceiver onComplete = new BroadcastReceiver() {
  49.         @Override
  50.         public void onReceive(Context context, Intent intent) {
  51.  
  52.             long referenceId = intent.getLongExtra(android.app.DownloadManager.EXTRA_DOWNLOAD_ID, -1);
  53.  
  54.             list.remove(referenceId);
  55.  
  56.             if (list.isEmpty()) {
  57.  
  58.                 NotificationCompat.Builder mBuilder =
  59.                         new NotificationCompat.Builder(mContext)
  60.                                 .setSmallIcon(R.mipmap.ic_launcher)
  61.                                 .setContentTitle("Halfdata")
  62.                                 .setContentText("Download completed");
  63.  
  64.                 NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
  65.                 notificationManager.notify(455, mBuilder.build());
  66.             }
  67.         }
  68.     };
  69.  
  70.  
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement