0

I am using a downloadmanager class inside my asynctask as i want to download files in a listview asynchronously. I followed following code from the link

Show Download progress inside activity using DownloadManager

I put that code inside the doInBackground in my AsyncTask.

 protected String doInBackground(String... f_url) {
         DownloadManager.Request request = new DownloadManager.Request(Uri.parse(file_url));

         request.setDescription("Testando");
         request.setTitle("Download");
         request.allowScanningByMediaScanner();
         request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
         request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "teste.zip");

          final DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);

         final long downloadId = manager.enqueue(request);

         new Thread(new Runnable() {

             @Override
             public void run() {

                 boolean downloading = true;

                 while (downloading) {

                     DownloadManager.Query q = new DownloadManager.Query();
                     q.setFilterById(downloadId);

                     Cursor cursor = manager.query(q);
                     cursor.moveToFirst();
                     int bytes_downloaded = cursor.getInt(cursor
                             .getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
                     int bytes_total = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));

                     if (cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS)) == DownloadManager.STATUS_SUCCESSFUL) {
                         downloading = false;
                     }

                     final int dl_progress = (bytes_downloaded *1001 ) / bytes_total;
                     runOnUiThread(new Runnable() {

                         @Override
                         public void run() {
                             publishProgress((int)dl_progress);
                             // mProgressBar.setProgress((int) dl_progress);

                         }
                     });
                 }

             }
         }).start();
         return null;
     }

But I keep on getting the following error

Error:(53, 61) error: cannot find symbol method getSystemService(String)

on this line

final DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);

i tried solving it through this link

Getting a cannot find symbol : method getSystemService from ant

but i dont know how to use it in my context. Please Help

Community
  • 1
  • 1
S.Khan
  • 167
  • 2
  • 12
  • getSystemService is a member function of Context. You need a Context to call it on (unless the class you're in is a Context, in which case you can call it on yourself). Since you're in an AsyncTask, you'll have to pass a Context to that task, then use that saved Context. – Gabe Sechan Mar 17 '16 at 20:52
  • @GabeSechan can you show me how? – S.Khan Mar 17 '16 at 20:53
  • Why are you using the DownloadManager within an AsyncTask? – Mike Scamell Mar 17 '16 at 20:57

0 Answers0