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以上携带数据跳转到其他应用

   
     
    

file_path的xml文件如下

    
     
     
    

你可能感兴趣的:(Android)