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