DownloadManager做下载操作案例

package com.lenovo.dh.saveaudio;

import androidx.appcompat.app.AppCompatActivity;

import android.app.DownloadManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import java.io.File;

public class MainActivity extends AppCompatActivity {
    Context mContext;
    Button btSave;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mContext = this;
        btSave = (Button)findViewById(R.id.btSave);
        btSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                    goDownload();
            }
        });
    }

    private void goDownload(){
        String url = "https://cloud.lenovomm.cn/lecloudfileapi/v1/download/1@21117379075334144015?sign=21fe449cc9b1adcdc90e382cb9e77177&ttl=-1&channelId=sync-push&ts=1596619765";
        new AndroidDownloadManager(mContext,url).setListener(new AndroidDownloadManagerListener() {
            @Override
            public void onPrepare() {

            }

            @Override
            public void onSuccess(String path) {
                Toast.makeText(mContext,"onSuccess() path:"+path,Toast.LENGTH_SHORT).show();
                Log.d("dh--","path:"+path);
            }

            @Override
            public void onFailed(Throwable throwable) {
                Toast.makeText(mContext,"onFailed path:"+throwable,Toast.LENGTH_SHORT).show();
            }
        }).downLoad();
    }


    public   class AndroidDownloadManager {
        private Context context;
        private DownloadManager downloadManager;
        private long downloadId;
        private String url;
        private String name;

        private String path;

        private AndroidDownloadManagerListener listener;

        public AndroidDownloadManager(Context context, String url) {
            this.context = context;
            this.url = url;
            this.name = getFileNameByUrl(url);
        }

        public AndroidDownloadManager setListener(AndroidDownloadManagerListener listener) {
            this.listener = listener;
            return this;
        }

        public void downLoad(){
            /**
             * 开始下载
             */
            DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
            //移动网络情况下是否允许漫游
            request.setAllowedOverRoaming(false);
            //在通知栏中显示,默认就是显示的
            request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE);
            request.setTitle("下载");
            request.setDescription("文件正在下载中......");
            request.setVisibleInDownloadsUi(true);
            //设置下载的路径
            //File  file =new File(context.getExternalFilesDir(Environment.DIRECTORY_PICTURES),name); //路径
            File file = new File(context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), name);
            request.setDestinationUri(Uri.fromFile(file));
            path = file.getAbsolutePath();
            //获取DownloadManager
            if (downloadManager == null) {
                downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
            }
            //将下载请求加入下载队列,加入下载队列后会给该任务返回一个long型的id,通过该id可以取消任务,重启任务、获取下载的文件等等
            if (downloadManager != null) {
                if (listener != null) {
                    listener.onPrepare();
                }
                downloadId = downloadManager.enqueue(request);
                downloadManager.remove(downloadId);
            }
            //注册广播接收者,监听下载状态
            context.registerReceiver(receiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
        }


        /**
         * 通过URL获取文件名
         *
         * @param url
         * @return
         */
        public  String getFileNameByUrl(String url) {
            String filename = url.substring(url.lastIndexOf("/") + 1);
            filename = filename.substring(0, filename.indexOf("?") == -1 ? filename.length() : filename.indexOf("?"));
            return filename;
        }


        //广播监听下载的各个状态
        private BroadcastReceiver receiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                DownloadManager.Query query = new DownloadManager.Query();
                //通过下载的id查找
                query.setFilterById(downloadId);
                Cursor cursor = downloadManager.query(query);
                if (cursor.moveToFirst()) {
                    int status = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS));
                    switch (status) {
                        //下载暂停
                        case DownloadManager.STATUS_PAUSED:
                            break;
                        //下载延迟
                        case DownloadManager.STATUS_PENDING:
                            break;
                        //正在下载
                        case DownloadManager.STATUS_RUNNING:
                            break;
                        //下载完成
                        case DownloadManager.STATUS_SUCCESSFUL:
                            if (listener != null) {
                                listener.onSuccess(path);
                            }
                            cursor.close();
                            context.unregisterReceiver(receiver);
                            break;
                        //下载失败
                        case DownloadManager.STATUS_FAILED:
                            if (listener != null) {
                                listener.onFailed(new Exception("下载失败"));
                            }
                            cursor.close();
                            context.unregisterReceiver(receiver);
                            break;
                    }
                }
            }
        };

    }

    public interface AndroidDownloadManagerListener {
        void onPrepare();

        void onSuccess(String path);

        void onFailed(Throwable throwable);
    }

}

 

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页