Android调用系统下载服务下载文件

下载代码如下:

    public void download() {
        String fileName = new File(getFilesDir(), "dl").getAbsolutePath();
        //文件下载链接
        String url = "";
        DownloadManager downloadManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
        DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
        // 通知栏的下载通知
        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
        request.setTitle(fileName);
        request.setMimeType("application/vnd.android.package-archive");
        File file = new File(getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), fileName);
        if (file.exists()) {
            file.delete();
        }
        request.setDestinationInExternalFilesDir(this, Environment.DIRECTORY_DOWNLOADS, fileName);
        long downloadId = downloadManager.enqueue(request);
        Log.d(TAG, "downloadId:" + downloadId);
        //文件下载完成会发送完成广播,可注册广播进行监听
        IntentFilter intentFilter = new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
        intentFilter.addAction(DownloadManager.ACTION_NOTIFICATION_CLICKED);
        intentFilter.addAction(DownloadManager.ACTION_VIEW_DOWNLOADS);
        mDownloadBroadcast = new DownloadBroadcast(file);
        registerReceiver(mDownloadBroadcast, intentFilter);
    }
/ **
 *  文件下载完成或点击通知栏广播

 */

    private class DownloadBroadcast extends BroadcastReceiver {

        private final File mFile;

        public DownloadBroadcast(File file) {
            mFile = file;
        }

        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            Logger.d("action:" + action);
            if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
                Intent intent1 = new Intent(Intent.ACTION_VIEW);
                intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
                    intent1.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                    Uri uri1 = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".fileProvider", mFile);
                    intent1.setDataAndType(uri1, "application/vnd.android.package-archive");
                } else {
                    intent1.setDataAndType(Uri.fromFile(mFile), "application/vnd.android.package-archive");
                }
                Logger.d("mFile:" + mFile);
                try {
                    startActivity(intent1);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

最后不要忘了界面销毁的时候反注册广播

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mDownloadBroadcast != null) {
            unregisterReceiver(mDownloadBroadcast);
        }
    }

如果是下载apk文件,下载完成后需要调起安装应用安卓7.0以上还要做以下配置

AndroidManifest中注册provider如下,用于7.0以上携带数据跳转到其他应用

   <provider
    android:name="android.support.v4.content.FileProvider"
     android:authorities="${applicationId}.fileProvider"
     android:exported="false"
     android:grantUriPermissions="true">
     <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/file_path"/>
    </provider>

file_path的xml文件如下

    <?xml version="1.0" encoding="utf-8"?>
     <paths xmlns:android="http://schemas.android.com/apk/res/android">
     <external-path name="external_files" path="."/>
    </paths>

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值