Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.android.jsondownload;
- import android.app.DownloadManager;
- import android.app.NotificationManager;
- import android.content.BroadcastReceiver;
- import android.content.Context;
- import android.content.Intent;
- import android.content.IntentFilter;
- import android.net.Uri;
- import android.os.Environment;
- import android.support.v4.app.NotificationCompat;
- import android.widget.Button;
- import java.util.ArrayList;
- public class Downloader {
- private DownloadManager downloader;
- ArrayList<Long> list = new ArrayList<>();
- private long refid;
- private Button mbtnStartDownload;
- private Context mContext;
- public Downloader(Context context) {
- this.mContext = context;
- }
- public void startDownload(String url, int i){
- downloader = (DownloadManager) mContext.getSystemService(Context.DOWNLOAD_SERVICE);
- mContext.registerReceiver(onComplete, new IntentFilter(android.app.DownloadManager.ACTION_DOWNLOAD_COMPLETE));
- android.app.DownloadManager.Request request = new android.app.DownloadManager.Request(Uri.parse(url));
- request.setAllowedNetworkTypes(android.app.DownloadManager.Request.NETWORK_WIFI | android.app.DownloadManager.Request.NETWORK_MOBILE);
- request.setAllowedOverRoaming(false);
- request.setTitle("Downloading " + "Halfdata_" + i + ".zip");
- request.setDescription("Halfdata_" + i + ".zip");
- request.setVisibleInDownloadsUi(true);
- request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "/halfdatas/" + "/" + "halfdata_" + i + ".zip");
- refid = downloader.enqueue(request);
- list.add(refid);
- }
- private BroadcastReceiver onComplete = new BroadcastReceiver() {
- @Override
- public void onReceive(Context context, Intent intent) {
- long referenceId = intent.getLongExtra(android.app.DownloadManager.EXTRA_DOWNLOAD_ID, -1);
- list.remove(referenceId);
- if (list.isEmpty()) {
- NotificationCompat.Builder mBuilder =
- new NotificationCompat.Builder(mContext)
- .setSmallIcon(R.mipmap.ic_launcher)
- .setContentTitle("Halfdata")
- .setContentText("Download completed");
- NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
- notificationManager.notify(455, mBuilder.build());
- }
- }
- };
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement