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;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Button button = (Button) findViewById(R.id.button);button.setOnClickListener(new View.OnClickListener() {@Overridepublic 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() {@Overridepublic 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>


更多相关文章

  1. android存取数据方式:文件、SharedPreferences
  2. 用android-logging-log4j去实现log输出内容到sd卡中的文件的功能
  3. Android解析XML文件的三种方式
  4. android中读XML文件
  5. android带进度的文件上传
  6. Android——自定义通知栏使用
  7. android-获取手机电话的状态
  8. android 查看解压后的.xml文件代码(axmlprinter2)
  9. 文件编码的测试(android)

随机推荐

  1. Android(安卓)获取系统和应用程序
  2. 代码设置EditText只输入数字、字母
  3. Android(安卓)ConditionVariable的用法
  4. Android中加载PNG图片时出现错误----No r
  5. Android内存泄漏剖析之AsyncTask
  6. Android(安卓)View的事件传递机制
  7. Android(安卓)PullToRefresh(下拉刷新)的使
  8. Android(安卓)Google Calendar 日曆同步
  9. Android(安卓)NestedScroll嵌套滑动机制
  10. AndroidStudio3.x 打开Android(安卓)Devi