我在asynctask中使用了downloadmanager类,因为我想异步下载listview中的文件。我按照链接中的代码
进行了操作Show Download progress inside activity using DownloadManager
我把那些代码放在我的AsyncTask中的doInBackground中。
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;
}
但我继续收到以下错误
Error:(53, 61) error: cannot find symbol method getSystemService(String)
在这一行
final DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
我尝试通过此链接解决它
Getting a cannot find symbol : method getSystemService from ant
但我不知道如何在我的上下文中使用它。请帮忙