DownloadManager的使用

    DownloadManager是系统开放给第三方应用使用的类,包含两个静态内部类DownloadManager.Query和DownloadManager.Request。DownloadManager.Request用来请求一个下载,DownloadManager.Query用来查询下载信息。具体使用代码如下:

 

  1 package com.example.administrator.mystudent.downloadManager;
  2 
  3 import android.app.Activity;
  4 import android.app.DownloadManager;
  5 import android.content.BroadcastReceiver;
  6 import android.content.Context;
  7 import android.content.Intent;
  8 import android.content.IntentFilter;
  9 import android.database.Cursor;
 10 import android.net.Uri;
 11 import android.os.Environment;
 12 import android.support.v7.app.AppCompatActivity;
 13 import android.os.Bundle;
 14 import android.util.Log;
 15 import android.view.View;
 16 import android.widget.AdapterView;
 17 import android.widget.Button;
 18 import android.widget.TextView;
 19 import android.widget.Toast;
 20 
 21 import com.example.administrator.mystudent.R;
 22 
 23 import java.util.HashMap;
 24 import java.util.Map;
 25 
 26 public class DownLoadActivity extends Activity {
 27     private Button starButton;
 28     private Button stopButton;
 29     private Button dirButton;
 30     private TextView infoText;
 31 
 32     private DownloadManager downloader;
 33     private Uri uri;
 34     private long reference;
 35 
 36     @Override
 37     protected void onCreate(Bundle savedInstanceState) {
 38         super.onCreate(savedInstanceState);
 39         setContentView(R.layout.activity_down_load);
 40 
 41         initView();
 42     }
 43 
 44     private void initView() {
 45         starButton = (Button) findViewById(R.id.star);
 46         stopButton = (Button) findViewById(R.id.stop);
 47         dirButton = (Button) findViewById(R.id.dir);
 48         infoText= (TextView) findViewById(R.id.myText);
 49 
 50         starButton.setOnClickListener(new myButtonListener());
 51         stopButton.setOnClickListener(new myButtonListener());
 52         dirButton.setOnClickListener(new myButtonListener());
 53     }
 54 
 55     class myButtonListener implements View.OnClickListener {
 56         @Override
 57         public void onClick(View v) {
 58             switch (v.getId()) {
 59                 case R.id.star: {
 60                     //执行下载任务
 61                     downLoadManager();
 62                 }
 63                 break;
 64                 case R.id.stop: {
 65                     //停止下载
 66                     downloader.remove(reference);
 67                 }
 68                 break;
 69                 case R.id.dir: {
 70                     //获取存放地址
 71                     Map sDir=getMyFilesDir();
 72                     infoText.setText("downid:"+sDir.get("downid")
 73                             +"title:"+sDir.get("title")
 74                             +"statuss:"+sDir.get("statuss")
 75                             +"address:"+sDir.get("address")
 76                             +"status:"+sDir.get("status")
 77                     );
 78                 }
 79                 break;
 80             }
 81         }
 82     }
 83 
 84     /**
 85      * 获取下载的文件存贮路径
 86      * @return 文件路径
 87      */
 88     private Map getMyFilesDir() {
 89         Map<String, String> map = null;
 90         //创建查询对象
 91         DownloadManager.Query query=new DownloadManager.Query();
 92         //根据任务编号查询下载任务信息
 93         query.setFilterById(reference);
 94 
 95         Cursor cursor=downloader.query(query);
 96         while (cursor.moveToNext()){
 97             String downId= cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_ID));    //下载文件的id
 98             String title = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_TITLE)); //下载文件的题目
 99             String address = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI)); //下载的地址
100             String statuss = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS));   //状态
101             String size= cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR)); //大小
102             String sizeTotal = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES)); //总大小
103 
104             map = new HashMap<String, String>();
105             map.put("downid", downId);
106             map.put("title", title);
107             map.put("statuss", statuss);
108             map.put("address", address);
109             map.put("status", sizeTotal+":"+size);
110         }
111         cursor.close();
112         return map;
113     }
114 
115     /**
116      * DownloadManager的使用
117      */
118     private void downLoadManager() {
119         //创建downLoadManager的管理器
120         downloader = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
121         //创建一个URI
122         // uri = Uri.parse("http://biz.10039shop.com/download/leyt/leyt.apk");
123         uri = Uri.parse("http://p1.ifengimg.com/a/2017_09/75dd25f878e8715_size66_w600_h363.jpg");
124         //封装一个request对象
125         DownloadManager.Request request = new DownloadManager.Request(uri);
126         //设置文件必须在WIFI下下载
127         request.setAllowedNetworkTypes(request.NETWORK_WIFI);
128 
129         //用于设置下载时时候在状态栏显示通知信息
130         request.setNotificationVisibility(request.VISIBILITY_VISIBLE);
131         //设置通知栏标题
132         request.setTitle("百度下载");
133         //设置Notification的message信息
134         request.setDescription("图片正在下载");
135         //用于设置漫游状态下是否可以下载
136         request.setAllowedOverRoaming(false);
137         //设置文件存放目录
138         request.setDestinationInExternalFilesDir(this, Environment.DIRECTORY_DOWNLOADS, "myDownLoad");
139 
140         //发送request请求并返回一个下载ID(开始下载了)
141         reference = downloader.enqueue(request);
142         Log.e("开始下载", "下载的id是" + reference);
143 
144         //创建一个下载的广播,下载完成之后
145         IntentFilter intentFilter = new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
146 
147         BroadcastReceiver myReceiver = new BroadcastReceiver() {
148             @Override
149             public void onReceive(Context context, Intent intent) {
150                 long referenceTo = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
151                 if (referenceTo == reference) {
152                     Log.e("结束下载", "下载完成" + referenceTo);
153                     Toast.makeText(getApplicationContext(), "文件下载完成", Toast.LENGTH_SHORT).show();
154                 }
155             }
156         };
157         //注册广播
158         registerReceiver(myReceiver, intentFilter);
159     }
160 }

 

posted @ 2017-02-28 15:43  点滴之水  阅读(2534)  评论(0编辑  收藏  举报