android移动开发-单文件下载-基于官方DownLoadManager进行
2016-11-24 20:32
423 查看
代码取自Demo ,实现单文件下载并弹出打开方式,由于采用意图Intent方式,所以很多高度定制的Rom就可能进入异常了,我在后期会整合网络资源,整理出一个新的方案!
1-工具类,简单看一下 ,看懂了就好/** * Created by Administrator on 2016/11/23. */ public class DownLoadMng { /** * 得到一个经过基本初始化的请求实例 * * @param url * @return */ public static DownloadManager.Request getRequest(String url) { DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url)); //设置在什么网络情况下进行下载 request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI); //设置通知栏标题 request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE); request.setTitle("通知栏显示"); request.setDescription("通知栏下拉的条目上显示"); request.setAllowedOverRoaming(false); return request; } /** * 这个好用 * * @param activity activity引用,用于跳intent * @param path 文件路径 */ public static void openFile2(Activity activity, String path) { File f = new File(path); Intent intent = new Intent(); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setAction(android.content.Intent.ACTION_VIEW); /* 调用getMIMEType()来取得MimeType */ String type = getMIMEType(f); /* 设置intent的file与MimeType */ intent.setDataAndType(Uri.fromFile(f), type); try { activity.startActivity(intent); } catch (ActivityNotFoundException e) { Toast.makeText(context, "附件无法打开,请下载相关软件!", 500).show(); } } /* 判断文件MimeType的method */ private static String getMIMEType(File f) { String type = ""; String fName = f.getName(); /* 取得扩展名 */ String end = fName.substring(fName.lastIndexOf(".") + 1, fName.length()).toLowerCase(); /* 依扩展名的类型决定MimeType */ if (end.equals("m4a") || end.equals("mp3") || end.equals("mid") || end.equals("xmf") || end.equals("ogg") || end.equals("wav")) { type = "audio"; } else if (end.equals("3gp") || end.equals("mp4")) { type = "video"; } else if (end.equals("jpg") || end.equals("gif") || end.equals("png") || end.equals("jpeg") || end.equals("bmp")) { type = "image"; } else if (end.equals("apk")) { /* android.permission.INSTALL_PACKAGES */ type = "application/vnd.android.package-archive"; } else if (end.equals("xls")) { type = "application/vnd.ms-excel"; } else if (end.equals("word") || end.contains("doc")) { type = "application/msword"; } /*如果无法直接打开,就跳出软件列表给用户选择 */ if (end.equals("apk")) { } else { type += "/*"; } return type; } /** * 这个有隐藏bug 不用了 并且下面的方法全引用自此方法 故不用 * * @param filePath * @return */ @Deprecated public static Intent openFile(String filePath) { File file = new File(filePath); if (!file.exists()) return null; /* 取得扩展名 */ String end = file.getName().substring(file.getName().lastIndexOf(".") + 1, file.getName().length()).toLowerCase(); /* 依扩展名的类型决定MimeType */ if (end.equals("m4a") || end.equals("mp3") || end.equals("mid") || end.equals("xmf") || end.equals("ogg") || end.equals("wav")) { return getAudioFileIntent(filePath); } else if (end.equals("3gp") || end.equals("mp4")) { return getAudioFileIntent(filePath); } else if (end.equals("jpg") || end.equals("gif") || end.equals("png") || end.equals("jpeg") || end.equals("bmp")) { return getImageFileIntent(filePath); } else if (end.equals("apk")) { return getApkFileIntent(filePath); } else if (end.equals("ppt")) { return getPptFileIntent(filePath); } else if (end.equals("xls")) { return getExcelFileIntent(filePath); } else if (end.equals("doc")) { return getWordFileIntent(filePath); } else if (end.equals("pdf")) { return getPdfFileIntent(filePath); } else if (end.equals("chm")) { return getChmFileIntent(filePath); } else if (end.equals("txt")) { return getTextFileIntent(filePath, false); } else { return getAllIntent(filePath); } } //Android获取一个用于打开APK文件的intent @Deprecated public static Intent getAllIntent(String param) { Intent intent = new Intent(); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setAction(android.content.Intent.ACTION_VIEW); Uri uri = Uri.fromFile(new File(param)); intent.setDataAndType(uri, "*/*"); return intent; } //Android获取一个用于打开APK文件的intent public static Intent getApkFileIntent(String param) { Intent intent = new Intent(); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setAction(android.content.Intent.ACTION_VIEW); Uri uri = Uri.fromFile(new File(param)); intent.setDataAndType(uri, "application/vnd.android.package-archive"); return intent; } //Android获取一个用于打开VIDEO文件的intent public static Intent getVideoFileIntent(String param) { Intent intent = new Intent("android.intent.action.VIEW"); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); intent.putExtra("oneshot", 0); intent.putExtra("configchange", 0); Uri uri = Uri.fromFile(new File(param)); intent.setDataAndType(uri, "video/*"); return intent; } //Android获取一个用于打开AUDIO文件的intent public static Intent getAudioFileIntent(String param) { Intent intent = new Intent("android.intent.action.VIEW"); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); intent.putExtra("oneshot", 0); intent.putExtra("configchange", 0); Uri uri = Uri.fromFile(new File(param)); intent.setDataAndType(uri, "audio/*"); return intent; } //Android获取一个用于打开Html文件的intent public static Intent getHtmlFileIntent(String param) { Uri uri = Uri.parse(param).buildUpon().encodedAuthority("com.android.htmlfileprovider").scheme("content").encodedPath(param).build(); Intent intent = new Intent("android.intent.action.VIEW"); intent.setDataAndType(uri, "text/html"); return intent; } //Android获取一个用于打开图片文件的intent public static Intent getImageFileIntent(String param) { Intent intent = new Intent("android.intent.action.VIEW"); intent.addCategory("android.intent.category.DEFAULT"); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); Uri uri = Uri.fromFile(new File(param)); intent.setDataAndType(uri, "image/*"); return intent; } //Android获取一个用于打开PPT文件的intent public static Intent getPptFileIntent(String param) { Intent intent = new Intent("android.intent.action.VIEW"); intent.addCategory("android.intent.category.DEFAULT"); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); Uri uri = Uri.fromFile(new File(param)); intent.setDataAndType(uri, "application/vnd.ms-powerpoint"); return intent; } //Android获取一个用于打开Excel文件的intent public static Intent getExcelFileIntent(String param) { Intent intent = new Intent("android.intent.action.VIEW"); intent.addCategory("android.intent.category.DEFAULT"); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); Uri uri = Uri.fromFile(new File(param)); intent.setDataAndType(uri, "application/vnd.ms-excel"); return intent; } //Android获取一个用于打开Word文件的intent public static Intent getWordFileIntent(String param) { Intent intent = new Intent("android.intent.action.VIEW"); intent.addCategory("android.intent.category.DEFAULT"); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); Uri uri = Uri.fromFile(new File(param)); intent.setDataAndType(uri, "application/msword"); return intent; } //Android获取一个用于打开CHM文件的intent public static Intent getChmFileIntent(String param) { Intent intent = new Intent("android.intent.action.VIEW"); intent.addCategory("android.intent.category.DEFAULT"); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); Uri uri = Uri.fromFile(new File(param)); intent.setDataAndType(uri, "application/x-chm"); return intent; } //Android获取一个用于打开文本文件的intent public static Intent getTextFileIntent(String param, boolean paramBoolean) { Intent intent = new Intent("android.intent.action.VIEW"); intent.addCategory("android.intent.category.DEFAULT"); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); if (paramBoolean) { Uri uri1 = Uri.parse(param); intent.setDataAndType(uri1, "text/plain"); } else { Uri uri2 = Uri.fromFile(new File(param)); intent.setDataAndType(uri2, "text/plain"); } return intent; } //Android获取一个用于打开PDF文件的intent public static Intent getPdfFileIntent(String param) { Intent intent = new Intent("android.intent.action.VIEW"); intent.addCategory("android.intent.category.DEFAULT"); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); Uri uri = Uri.fromFile(new File(param)); intent.setDataAndType(uri, "application/pdf"); return intent; } }
2、开始下载
private void downLoad(){ DownloadManager.Request request = DownLoadMng.getRequest(url); //设置文件存放目录 path = "/yangguang";//Environment.getExternalStorageDirectory().getAbsolutePath() isFolderExist(path);//文件夹判空 filename = accessmodel.getData().getFile_list().get(position).getTitle(); request.setDestinationInExternalPublicDir(path, filename); absolutePath = path + "/" + filename; downManager = (DownloadManager) getActivity().getSystemService(Context.DOWNLOAD_SERVICE); id = downManager.enqueue(request); getActivity().registerReceiver(receiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));// 监听下载状态 } private BroadcastReceiver receiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { //这里可以取得下载的id,这样就可以知道哪个文件下载完成了。适用与多个下载任务的监听 Log.v("intent", "" + intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0)); queryDownloadStatus(); } }; private void queryDownloadStatus() { DownloadManager.Query query = new DownloadManager.Query(); query.setFilterById(id); Cursor c = downManager.query(query); if (c.moveToFirst()) { int status = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS)); switch (status) { case DownloadManager.STATUS_PAUSED: Log.v("debug", "STATUS_PAUSED"); case DownloadManager.STATUS_PENDING: Log.v("debug", "STATUS_PENDING"); case DownloadManager.STATUS_RUNNING: //正在下载,不做任何事情 Log.v("debug", "下载中"); break; case DownloadManager.STATUS_SUCCESSFUL: //完成 showToast("下载完成"); try { DownLoadMng.openFile2(getActivity(), absolutePath); } catch (Exception e) { e.printStackTrace(); DebugLogUtil.getInstance().Error("打开失败~" + e); } break; case DownloadManager.STATUS_FAILED: //清除已下载的内容,重新下载 showToast("下载失败"); // Log.v("debug", "STATUS_FAILED"); // downManager.remove(id); // id.edit().clear().commit(); break; } } }
相关文章推荐
- android开发(23)使用DownloadManager下载文件
- 利用Android自带的DownloadManager实现文件下载进行apk在线升级
- phonegap 开发入门 PhoneGap官方网站上有详细的入门示例教程,这里,我针对使用PhoneGap进行Android移动应用的开发对其官网的Get Started进行一些介绍、补充。 Ste
- android开发(23)使用DownloadManager下载文件
- android开发(23)使用DownloadManager下载文件
- Android开发之网络请求通信专题(二):基于HttpClient的文件上传下载
- mars老师[Android开发视频教学].01_18 文件下载 源码
- android开发之下载文本、音乐文件
- Android 开发中的网络下载策略 与 文件缓存策略
- Android开发学习总结(1.18-文件下载)
- Android应用开发之使用Socket进行大文件断点上传续传
- Android开发视频教程之十八(文件下载)
- Android开发九:从网上下载文件并存储到SD卡中
- android开发下载的文件大小始终大于服务器的文件的大小问题解决
- android开发 音乐,文件下载
- 使用libcurl进行文件下载类项目开发(一) libcurl简单使用介绍
- 使用Android自带DownloadManager下载文件
- android 开发之文件下载
- android开发(7) 文件下载
- [转]最新Android开发工具官方下载地址