Android 文件重定向下载 & 通知问题小结

小菜之前在 Android 处理文件下载过程中遇到以下几个小问题,小菜简单整理一下;

Download

重定向文件下载如何获取文件类型?

小菜在下载过程中通常需要获取文件名android开发框架称和文件类型等进android开发环境行具体的业务处理;而下载类的链接也不是固定格式的,主要区分为 https://github.com/ace.../test.apandroid开发k 以及 https://github.com/ace...?app=acetest 等经过重定向之后的下载链接;

针对第一种类型链接android开发工程师,小菜可以android开发语言方便的获取文件类型和名称等一系列信息,针对第二种重定向类型链接,小菜尝试了如下几种方式;

方案一:

小菜尝试通过 BufferedInputStream 获取文件类型,其中调用时需要进行异步操作,而结果并不如意,很多文件类型不能直接识别;

privaandroid开发工程师te String getFileType(String path) {
String type = "";
BufferedInputStream bufferedInputStream = nullandroid开发语言;
Httpandroid开发工程师URLConnection urlconnection = null;android开发语言
try {
URL url = new URL(path);
uandroid开发工程师需要学什么rlconnectandroid开发者ionandroid开发工程师 = (HttpURLConnection) url.openConnectioandroid开发语言n();
urlconnection.candroid开发艺术探索onandroid开发工具nect();
bufferedInputStream = new BufferedInputStream(urlconnandroid开发框架ection.getInputStream());
type = HttpURLConnection.guessContentTypeFromStream(bufferedInputStream) + "android开发框架";
} catch (IOException e) {
e.printStackTraceandroid开发复杂();
}
return type;
}

方案二:

小菜借助 OKHtandroid开发者tp 方式将重定向android开发工程师需要学什么URL 转为起始状态 URL,从而获取文件名称,文件类型等;但该方式调用时也需要异步操作,不能在主线程中执行;

private void getRealUrl(String url) {
if (!Empty.check(url)) {
try {
OkHttpClientandroid开发工具 candroid开发环境lient = new OkHttpClient();
Request request = new Request.android开发工程师Builder().url(url).build();
Response response = clienandroid开发复杂t.newCall(request).execute();
HttpUandroid开发者rl realandroid开发Url = response.request().url();
if (!Empty.check(realUrl) && !Empty.check(realUrl.url()android开发)) {
String temp = realUrl.toString();
String fileName = temp.subsandroid开发tandroid开发ring(temp.landroid开发框架astandroid开发工程师IndexOf("/") + 1);
if (fileName.contains("?")) {
fileName = fileName.substring(0, fileName.lastIndexOf("?"));
}
...
}
} catch (IOException e) {
Loggeandroid开发环境r.e(TAG, Emandroid开发入门教程pty.checkandroid开发语言(e) ? "" : e.getMessandroid开发工程师age());
}
}
}

方案三:

在具体特定 WebVandroid开发入门教程iew 场合,可以通过 WebView 预先加载之后获取起始下载链接,之后在进行具体的业务逻辑操作;

小菜尝试了多种方式,对于重定向类型下载链接基本都需要异步耗时操作,暂时还未找到更简单快捷的方android开发式;

Notification

Notificaandroid开发tion 在日常应用场景非常多,而配合下载类提示用户时小菜遇到几个小问题,简单整理一下;

1. 使用进度条时提示音一直播放?

小菜测试时,使用进度条 setProgress 时,随着进度的进行提示音一直在提醒,此时可以设置 Notiandroid开发入门教程ficationCompat.Builder.setOnlyAlertOnce 只提醒一次即可;于此同事,部分手机时间一直在闪动,例如:刚刚android开发框架直在提示,可android开发工具以通过 setShowWhen 关闭时间戳提示即可;

NotificationCompat.Builder notification =
new NotificationCompat.Builder(context, notifyId + "").setSmallIandroid开发工程师需要学什么con(R.drawable.icon)
.setSound(null)
.setVibrate(null)
.setContentTitle(title)
.setContentText(dandroid开发工程师es)
.setAutoCancel(true)
.setShowWhen(false)
.setOnlyAlertOnce(true)
.setProgress(100, downloadProgresandroid开发语言s, false);

2. 结束后点击通知栏消息不消失?

小菜测试在设置点击自动关闭属性 setAutoCaandroid开发框架ncel 后,完成下载,点击通知栏消息时,该 Notification 未消失;其原因在于小android开发工程师菜省略了设置 setContentIntentPendingInandroid开发工程师需要学什么tent;即便是不需要跳转安装android开发环境或其他具体页面,也需要设置一个默认PendingIntent

Pendinandroid开发工程师需要学什么gIntent pendingIntent;
if (fileType != nulandroid开发工具l && fileType.equals(FileDownandroid开发语言loadManager.FILE_TYPE_APK)) {
pendingIandroid开发语言ntent = PendingIntent.getActivity(context, 0, clickIntent, 0);
} else {
pendingIntent = PendingIntent.getBroadcast(context, REQUESTCODE, clicandroid开发语言kIntent, PendingIntent.FLAGandroid开发复杂_UPDATE_CURRENT);
}
NotificationCompat.Builder notification =
nandroid开发艺术探索ew Notificandroid开发者ationCompaandroid开发复杂t.Builder(context, notifyId + "").setSmallIcon(R.drawable.iandroid开发con)
.setContentText(des)android开发艺术探索
.setContentIntent(android开发框架pendingIntent);

3. 如何左右滑动清除通知监听?

小菜之前未尝试过滑动清除 Nandroid开发艺术探索otification,实际与设置点击通知操作类似,也需要设置对应的 PendingIntentsetDeleteIntent 即可;

pendingIntent = PendingIntenandroid开发工程师t.getBroadcast(contextandroid开发入门教程, REQandroid开发复杂UESTCODE, clickIntent, PendingIntent.FLAG_android开发工具UPDATE_CURRENT);
Notiandroid开发工程师需要学什么ficationCompat.Builder notification =
new Notifiandroid开发工程师cationCompat.Builder(conteandroid开发复杂xt, notiandroid开发环境fyId + "").setSmallIcon(R.drawabandroid开发语言le.icon)
.setContentText(des)
.setDeleteIntenandroid开发复杂t(pendiandroid开发工程师ngIntent);

小菜在测试过程中,学习了很多之前不常用的属性,内容都很简单,小菜不做具体的介绍;主要是对于重定向文件下载的一个小积累;如有错误,请多多指导!

来源: 阿策小和尚