android DownloadManager实现下载显示进度条
时间: 2023-11-26 09:41:26 浏览: 172
要实现在Android应用程序中下载文件并同时显示进度条,可以使用Android DownloadManager类。 DownloadManager是Android系统提供的一个下载管理器,它可以轻松地执行文件下载,并且可以方便地显示进度条。
以下是实现此功能的步骤:
1. 添加权限
在AndroidManifest.xml文件中添加以下权限:
```
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.DOWNLOAD_WITHOUT_NOTIFICATION" />
```
2. 创建DownloadManager请求
创建一个DownloadManager.Request对象,设置下载请求的URL、下载文件的名称和类型。
```
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.setTitle("下载文件");
request.setDescription("正在下载");
request.setMimeType("application/pdf");
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "file.pdf");
```
3. 获取DownloadManager实例
获取DownloadManager实例,并将DownloadManager.Request对象传递给enqueue()方法。
```
DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
long downloadId = manager.enqueue(request);
```
4. 显示进度条
使用一个ProgressDialog对象来显示下载进度条,通过查询DownloadManager的状态来更新进度条的值。
```
ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setMessage("正在下载");
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setCancelable(true);
progressDialog.show();
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 bytesDownloaded = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
int bytesTotal = 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 = (int) ((bytesDownloaded * 100l) / bytesTotal);
runOnUiThread(new Runnable() {
@Override
public void run() {
progressDialog.setProgress(dl_progress);
}
});
cursor.close();
}
}
}).start();
```
在以上代码中,我们启动一个新线程来查询下载进度,并使用runOnUiThread()方法更新ProgressDialog对象的进度条。
完整代码示例:
```
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String url = "http://www.example.com/file.pdf";
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.setTitle("下载文件");
request.setDescription("正在下载");
request.setMimeType("application/pdf");
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "file.pdf");
DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
long downloadId = manager.enqueue(request);
ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setMessage("正在下载");
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setCancelable(true);
progressDialog.show();
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 bytesDownloaded = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
int bytesTotal = 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 = (int) ((bytesDownloaded * 100l) / bytesTotal);
runOnUiThread(new Runnable() {
@Override
public void run() {
progressDialog.setProgress(dl_progress);
}
});
cursor.close();
}
}
}).start();
}
}
```
阅读全文