下载代码如下:

    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文件如下

    <?xml version="1.0" encoding="utf-8"?>              

更多相关文章

  1. android上传文件至服务器
  2. Android写文件到Sd卡的一般过程
  3. android 实现流媒体播放远程mp3文件代码
  4. android -- 小功能 Android为多媒体文件生成缩略图
  5. Android, 如何在C文件中加log
  6. Android 删除 未接来电 通知
  7. Android文件夹大小
  8. Android中的多种文件读写操作方法

随机推荐

  1. Android Uri
  2. Android——permission之android:protect
  3. 为什么Android的Adapter中,bindview被调用
  4. Android(安卓)JNI入门第二篇――Java参数
  5. android 增加鼠标事件
  6. Android(安卓)通过zygote来运行java程序
  7. Android UI(2)Getting Started - Support
  8. Android震动器Vibrator调用
  9. Android布局LinearLayout+RelativeLayout
  10. android 开发书籍打包下载(包含十几本Andr