Android DownloadManager 的使用

public static long downloadMusic(String url, String artist, String song){
        Uri uri = Uri.parse(url);
        DownloadManager.Request request = new DownloadManager.Request(uri);
        String mp3FileName = FileUtils.getMp3FileName(artist, song);

        //设置下载文件存放路径
        request.setDestinationInExternalPublicDir(FileUtils.getRelativeMusicDir(), mp3FileName);

        //设置MimeType用于响应点击下载完后响应的事件
        request.setMimeType(MimeTypeMap.getFileExtensionFromUrl(url));

        //表示允许MediaScanner扫描到这个文件,默认不允许
        request.allowScanningByMediaScanner();

        //设置允许下载的数据类型
        request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE | DownloadManager.Request.NETWORK_WIFI);

        //移动网络情况下是否允许漫游,不允许
        request.setAllowedOverRoaming(false);

        DownloadManager downloadManager = (DownloadManager) AppCache.getContext().getSystemService(Context.DOWNLOAD_SERVICE);
        return downloadManager.enqueue(request);

    }

你可能感兴趣的:(安卓踩坑日记)