版权声明
本文来自博客园,作者:观心静 ,转载请注明原文链接:https://www.cnblogs.com/guanxinjing/p/13299949.html
本文版权归作者和博客园共有,欢迎转载,但必须给出原文链接,并保留此段声明,否则保留追究法律责任的权利。
前言
DownloadManager是Android系统自带的下载管理工具,此工具可以很好的调度好下载。在没有特殊需求的情况下,一般是推荐使用此工具下载的。另外这个工具下载还有有优势就是可以在下载app完成后直接跳转到安装页面。
参考:https://www.jianshu.com/p/e0496200769c
需要的权限
需要网络权限和文件读写权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.INTERNET"/>
请求安装包权限
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
创建下载请求
private void down(){ DownloadManager downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE); DownloadManager.Request request = new DownloadManager.Request(Uri.parse("https://dl.hdslb.com/mobile/latest/iBiliPlayer-html5_app_bili.apk"));//添加下载文件的网络路径 request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "iBiliPlayer-html5_app_bili.apk");//添加保存文件路径与名称 request.setTitle("测试下载");//添加在通知栏里显示的标题 request.setDescription("下载中");//添加在通知栏里显示的描述 request.addRequestHeader("token","11");//如果你的下载需要token,或者有秘钥要求,可以在此处添加header的 key: value request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE | DownloadManager.Request.NETWORK_WIFI);//设置下载的网络类型 request.setVisibleInDownloadsUi(false);//是否显示下载 从Android Q开始会被忽略 request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);//下载中与下载完成后都会在通知中显示| 另外可以选 DownloadManager.Request.VISIBILITY_VISIBLE 仅在下载中时显示在通知中,完成后会自动隐藏 long id = downloadManager.enqueue(request);//加入队列,会返回一个唯一下载id }
加入列队后,下载就进行了。我们可以在下拉通知栏里看到正在下载的文件。
另外注意 setDestinationInExternalPublicDir方法已经包含根目录路径了,所以直接传入DIRECTORY_DOWNLOADS就可以了,千万不要在导入一个 Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getPath() 这样他会自动在创建多层目录
查询即将保存的下载文件路径与文件名称
private void queryFileName(long id){ DownloadManager downloadManager = (DownloadManager) MainActivity.this.getApplication().getSystemService(Context.DOWNLOAD_SERVICE); DownloadManager.Query query = new DownloadManager.Query(); query.setFilterById(id); Cursor cursor = downloadManager.query(query); if (cursor.moveToFirst()) { String file = cursor.getString(cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_LOCAL_URI)); Log.e("调试_临时_log", "this_" + file); } }
监听下载状态
AppDownReceiver appDownReceiver = new AppDownReceiver(); registerReceiver(appDownReceiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
广播部分代码
public class DownloadReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (!TextUtils.equals(action, DownloadManager.ACTION_DOWNLOAD_COMPLETE)){ return; } long downloadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1); if (downloadId == -1){ return; } DownloadManager downloadManager = (DownloadManager) context.getApplicationContext().getSystemService(Context.DOWNLOAD_SERVICE); int status = getDownloadStatus(downloadManager, downloadId); if (status != DownloadManager.STATUS_SUCCESSFUL){ //下载状态不等于成功就跳出 return; } Uri uri = downloadManager.getUriForDownloadedFile(downloadId);//获取下载完成文件uri if (uri == null){ return; } } /** * 获取下载状态 * @param downloadManager * @param downloadId * @return */ private int getDownloadStatus(DownloadManager downloadManager, long downloadId) { DownloadManager.Query query = new DownloadManager.Query().setFilterById(downloadId); Cursor c = downloadManager.query(query); if (c != null) { try { if (c.moveToFirst()) { return c.getInt(c.getColumnIndexOrThrow(DownloadManager.COLUMN_STATUS)); } } finally { c.close(); } } return -1; } }
下载状态查询
/** * 获取下载状态 * * @param downloadId an ID for the download, unique across the system. * This ID is used to make future calls related to this download. * @return int * @see DownloadManager#STATUS_PENDING 下载等待开始时 * @see DownloadManager#STATUS_PAUSED 下载暂停 * @see DownloadManager#STATUS_RUNNING 正在下载中 * @see DownloadManager#STATUS_SUCCESSFUL 下载成功 * @see DownloadManager#STATUS_FAILED 下载失败 */ public int getDownloadStatus(long downloadId) { DownloadManager.Query query = new DownloadManager.Query().setFilterById(downloadId); Cursor c = downloadManager.query(query); if (c != null) { try { if (c.moveToFirst()) { return c.getInt(c.getColumnIndexOrThrow(DownloadManager.COLUMN_STATUS)); } } finally { c.close(); } } return -1; }
下载进度查询
/** * 获取当前下载进度 * * @return */ private int getDownloadProgress() { DownloadManager.Query query = new DownloadManager.Query().setFilterById(mDownloadId); Cursor c = mDownloadManager.query(query); if (c != null) { try { if (c.moveToFirst()) { return c.getInt(c.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR)); } } finally { c.close(); } } return -1; } /** * 获取下载总大小 * * @return */ private int getDownloadTotal() { DownloadManager.Query query = new DownloadManager.Query().setFilterById(mDownloadId); Cursor c = mDownloadManager.query(query); if (c != null) { try { if (c.moveToFirst()) { return c.getInt(c.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES)); } } finally { c.close(); } } return -1; }
安装下载完成的App
/** * 安装apk */ private void installApk(long id) { Uri uri = mDownloadManager.getUriForDownloadedFile(id); Intent install = new Intent(Intent.ACTION_VIEW); install.setDataAndType(uri, "application/vnd.android.package-archive"); install.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_GRANT_READ_URI_PERMISSION); getApplication().startActivity(install); }
END
本文来自博客园,作者:观心静 ,转载请注明原文链接:https://www.cnblogs.com/guanxinjing/p/13299949.html
本文版权归作者和博客园共有,欢迎转载,但必须给出原文链接,并保留此段声明,否则保留追究法律责任的权利。