大佬教程收集整理的这篇文章主要介绍了Android基于自带的DownloadManager实现下载功能示例,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
本文实例讲述了Android基于自带的DownloadManager实现下载功能。分享给大家供大家参考,具体如下:
DownloadManager.request request = new DownloadManager.request(Uri.parse(APK_URL)); request.setDesTinationInExternalPublicDir(DOWNLOAD_FOLDER_NAME,DOWNLOAD_file_name); request.settitle(getString(R.String.download_notification_titlE)); request.setDescription("meilishuo desc"); request.setNotificationVisibility(DownloadManager.request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); request.setVisibleInDownloadsUi(false); // request.allowScAnningBymediaScAnner(); // request.setAllowedNetworkTypes(DownloadManager.request.NETWORK_WIFI); // request.setShowRunningNotification(false); // request.setNotificationVisibility(DownloadManager.request.VISIBILITY_HIDDEN); request.setMimeType("application/cn.trinea.download.file"); downloadId = downloadManager.enqueue(request);
downloadManager.enqueue
是加入下载请求到下载管理类中,并且会返回一个下载的ID。
大文件只能在wifi下下载
class CompleteReceiver extends BroadcastReceiver { @Override public void onReceive(Context context,Intent intent) { /** * get the id of download which have download success,if the id is my id and it's status is successful,* then install it **/ long completeDownloadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID,-1); if (completeDownloadId == downloadId) { initData(); updateView(); // if Download successful,install apk if (downloadManagerPro.getStatusById(downloadId) == DownloadManager.STATUS_succesSFUL) { String apkFilePath = new StringBuilder(Environment.getExternalStorageDirectory().getAbsolutePath()) .append(File.separator).append(DOWNLOAD_FOLDER_Name).append(File.separator) .append(DOWNLOAD_file_name).toString(); install(context,apkFilePath); } } } };
这里的intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID,-1);
的DownloadManager.EXTRA_DOWNLOAD_ID
是DownloadManager类里的参数,使用下面方法注册广播
/** register download success broadcast **/ registerReceiver(completeReceiver,new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
用到的IntentFilter是下载完成的Filter
然后会通知这个广播,并且返回的intent里面包含了DownloadManager.EXTRA_DOWNLOAD_ID
的参数。
关于DownloadManager的其他用法可以查看@L_696_12@
这里再介绍下DownloadManager.Query的用法。
显而易见Query是内部类。有query.setFilterById
和query.setFilterByStatus
两个方法,
query.setFilterById就是把downloadManager.enqueue返回的id放进去作为查询的条件;
这里用的Query查询Downloads的数据库,但是只可以查询本应用下载的数据
/** * 使用DownloadManager.Query查询Downloads的DB,但是在stackoverflow中的解释是 * You can't access this DB from my application. For your own downloads,* use DownloadManager.Query to check on your download status. Data about downloads is not shared to other apps. */
所以要是想通过DownloadManager.Query
查询系统所有的下载记录是不可行的。
记得ApiDemo里有用户联系人使用Uri的方式查询联系人contacts,进入Root Explore观察com.android.providers.downloads包里的DB数据库内容时,发现下载的记录里有content://media/external/file/452122
测试下返回的字段
/*String[] downNames = cursor.getcolumnNames(); String str = ""; for(int i=0;i<downNames.length;i++){ str += downNames[i]+","; }*/ if(cursor!=null&&cursor.moveToLast()){ // cursor.moveToPrevIoUs() String displayname = cursor.getString(cursor.getcolumnIndex("_display_name")); String name = cursor.getString(cursor.getcolumnIndex("name")); String title = cursor.getString(cursor.getcolumnIndex("title")); String description = cursor.getString(cursor.getcolumnIndex("description")); String data = cursor.getString(cursor.getcolumnIndex("_data")); String bucket = cursor.getString(cursor.getcolumnIndex("bucket_display_name")); downloadTip.setText(displayname+","+name+","+title+","+description+","+data+","+bucket); }
根据自己需求提取字段。
更多关于Android相关内容感兴趣的读者可查看本站专题:《Android基本组件用法总结》、《Android开发入门与进阶教程》、《Android布局layout技巧总结》、《Android视图View技巧总结》、《Android编程之activity操作技巧总结》、《Android资源操作技巧汇总》及《Android控件用法总结》
希望本文所述对大家Android程序设计有所帮助。
以上是大佬教程为你收集整理的Android基于自带的DownloadManager实现下载功能示例全部内容,希望文章能够帮你解决Android基于自带的DownloadManager实现下载功能示例所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。