且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Android DownloadManager下载状态查询(2)

更新时间:2021-12-07 02:46:57



Android DownloadManager下载状态查询(2)

在我写的前一篇文章中,
《Android大数据、断点续传、耗时下载之DownloadManager开发简介(1)》
文章链接地址:http://blog.csdn.net/zhangphil/article/details/48949027
大致简介了Android DownloadManager如何完成一个下载任务。这篇文章在前一篇文章的基础上,做一些小改动,增加对下载任务状态的查询。
现在给出全部源代码。
MainActivity.java文件:

package zhangphil.demo;

import android.app.Activity;
import android.app.DownloadManager;
import android.app.DownloadManager.Request;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {

	private DownloadManager downloadManager;
	private long Id;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		Button button = (Button) findViewById(R.id.button);
		button.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// remove将依据Id号取消相应的下载任务
				// 可批量取消,remove(id1,id2,id3,id4,...);
				downloadManager.remove(Id);
			}
		});

		downloadManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);

		// 假设从这一个链接下载一个大文件。
		Request request = new Request(
				Uri.parse("http://apkc.mumayi.com/2015/03/06/92/927937/xingxiangyi_V3.1.3_mumayi_00169.apk"));

		// 仅允许在WIFI连接情况下下载
		request.setAllowedNetworkTypes(Request.NETWORK_WIFI);

		// 通知栏中将出现的内容
		request.setTitle("我的下载");
		request.setDescription("下载一个大文件");
		// 下载过程和下载完成后通知栏有通知消息。
		request.setNotificationVisibility(Request.VISIBILITY_VISIBLE | Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);

		// 此处可以由开发者自己指定一个文件存放下载文件。
		// 如果不指定则Android将使用系统默认的
		// request.setDestinationUri(Uri.fromFile(new File("")));

		// 默认的Android系统下载存储目录
		request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "my.apk");

		// enqueue 开始启动下载...
		Id = downloadManager.enqueue(request);

		Button queryButton = (Button) findViewById(R.id.queryButton);
		queryButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				queryStatus();
			}
		});
	}

	// 根据DownloadManager下载的Id,查询DownloadManager某个Id的下载任务状态。
	private void queryStatus() {
		DownloadManager.Query query = new DownloadManager.Query();
		query.setFilterById(Id);
		Cursor cursor = downloadManager.query(query);

		String statusMsg = "";
		if (cursor.moveToFirst()) {
			int status = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS));
			switch (status) {
			case DownloadManager.STATUS_PAUSED:
				statusMsg = "STATUS_PAUSED";
			case DownloadManager.STATUS_PENDING:
				statusMsg = "STATUS_PENDING";
			case DownloadManager.STATUS_RUNNING:
				statusMsg = "STATUS_RUNNING";
				break;
			case DownloadManager.STATUS_SUCCESSFUL:
				statusMsg = "STATUS_SUCCESSFUL";
				break;
			case DownloadManager.STATUS_FAILED:
				statusMsg = "STATUS_FAILED";
				break;

			default:
				statusMsg = "未知状态";
				break;
			}

			Toast.makeText(getApplicationContext(), statusMsg, Toast.LENGTH_SHORT).show();
		}
	}
}


MainActivity.java需要的布局文件activity_main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="取消下载" />

    <Button
        android:id="@+id/queryButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="查询下载状态" />

</LinearLayout>