其他分享
首页 > 其他分享> > Android中的DownloadManager自带下载器

Android中的DownloadManager自带下载器

作者:互联网

//获取下载器
DownloadManager manager= (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
String url="下载地址";
Uri uri = Uri.parse(url);
//设置下载地址
DownloadManager.Request req = new DownloadManager.Request(uri);
//设置网络状态下进行更新 我这是移动网络和wifi 还有NETWORK_BLUETOOTH
req.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE | DownloadManager.Request.NETWORK_WIFI);
//下载中和下载完后都·不显示通知栏(不显示在状态栏需要额外添加权限"android.permission.DOWNLOAD_WITHOUT_NOTIFICATION")
req.setNotificationVisibility(DownloadManager.Request.VISIBILITY_HIDDEN);
//使用系统默认的下载路径 此处为应用内 /android/data/packages ,所以兼容7.0 8.0
req.setDestinationInExternalPublicDir(DOWNLOAD_PATH, Md5.getMD5(urlList.get(i).getUrl()) + "." + fileType);
//通知栏标题
req.setTitle(appName);
//通知栏描述信息
req.setDescription("下载中");
//设置类型为.apk
req.setMimeType("application/vnd.android.package-archive");
LogUtil.showLogE(TAG, "开始下载:" + urlList.get(i).getUrl());
manager.enqueue(req);
// 接受下载完成后的intent
public class DownloadCompleteReceiver extends BroadcastReceiver {
    @SuppressLint("NewApi")
    @Override
    public void onReceive(Context context, Intent intent) {
        //判断是否下载完成的广播
        if (intent.getAction().equals(DownloadManager.ACTION_DOWNLOAD_COMPLETE)) {
            //获取下载的文件id
            long downId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
            DownloadManager.Query query = new DownloadManager.Query();
            query.setFilterById(downId);

            Cursor cursor = manager.query(query);
            if (cursor != null && cursor.moveToFirst()) {
                int columnIndex = cursor.getColumnIndex(DownloadManager.COLUMN_STATUS);
                int status = cursor.getInt(columnIndex);
                LogUtil.showLogE(TAG, MemorySpaceCheck.getAvailableSize("/mnt/internal_sd" + DOWNLOAD_PATH) / 1024 / 1024 + "m");
                switch (status) {
                    case DownloadManager.STATUS_SUCCESSFUL:
                        Uri downloadFileUri = manager.getUriForDownloadedFile(downId);
                        EventBus.getDefault().post(new MessageEvent("下载成功", downloadFileUri.getPath()));
                        downNum++;
                        LogUtil.showLogE(TAG, "下载完成,当前下载数:" + downNum + "剩余下载数:" + (maxDown - downNum) + "下载路径:" + downloadFileUri.getPath());
                        break;
                    default:
                        EventBus.getDefault().post(new MessageEvent("下载失败"));
                        downNum++;
                        LogUtil.showLogE(TAG, "下载失败,当前下载数:" + downNum + "剩余下载数:" + (maxDown - downNum) + "失败id:" + downId);
                        break;
                }
            }
            if (downNum == maxDown) {
                LogUtil.showLogE(TAG, "下载结束,下载量:" + downNum);
                downNum = 0;
                EventBus.getDefault().post(new MessageEvent("下载完成"));
                if (receiver != null) {
                    unregisterReceiver(receiver);
                    receiver = null;
                }
                //停止服务并关闭广播
                DownloadService.this.stopSelf();
            }
        }
    }
}

标签:downNum,DownloadManager,req,showLogE,DOWNLOAD,自带,Android,下载
来源: https://blog.csdn.net/tomb363897221/article/details/88368577