博主信息
Sky
博文
291
粉丝
0
评论
0
访问量
7020
积分:0
P豆:617

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

2021年10月17日 22:53:41阅读数:12博客 / Sky

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

Download

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

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

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

方案一:

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

private String getFileType(String path) {
   String type = "";
   BufferedInputStream bufferedInputStream = null;
   HttpURLConnection urlconnection = null;
   try {
       URL url = new URL(path);
       urlconnection = (HttpURLConnection) url.openConnection();
       urlconnection.connect();
       bufferedInputStream = new BufferedInputStream(urlconnection.getInputStream());
       type = HttpURLConnection.guessContentTypeFromStream(bufferedInputStream) + "";
   } catch (IOException e) {
       e.printStackTrace();
   }
   return type;
}

方案二:

    小菜借助 OKHttp 方式将重定向的 URL 转为起始状态 URL,从而获取文件名称,文件类型等;但该方式调用时也需要异步操作,不能在主线程中执行;

private void getRealUrl(String url) {
   if (!Empty.check(url)) {
       try {
           OkHttpClient client = new OkHttpClient();
           Request request = new Request.Builder().url(url).build();
           Response response = client.newCall(request).execute();
           HttpUrl realUrl = response.request().url();
           if (!Empty.check(realUrl) && !Empty.check(realUrl.url())) {
               String temp = realUrl.toString();
               String fileName = temp.substring(temp.lastIndexOf("/") + 1);
               if (fileName.contains("?")) {
                   fileName = fileName.substring(0, fileName.lastIndexOf("?"));
               }
               ...
           }
       } catch (IOException e) {
           Logger.e(TAG, Empty.check(e) ? "" : e.getMessage());
       }
   }
}

方案三:

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

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

Notification

    Notification 在日常应用场景非常多,而配合手机游戏交易平台下载类提示用户时小菜遇到几个小问题,简单整理一下;

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

    小菜测试时,使用进度条 setProgress 时,随着进度的进行提示音一直在提醒,此时可以设置 NotificationCompat.Builder.setOnlyAlertOnce 只提醒一次即可;于此同事,部分手机时间一直在闪动,例如:刚刚 一直在提示,可以通过 setShowWhen 关闭时间戳提示即可;

NotificationCompat.Builder notification =
   new NotificationCompat.Builder(context, notifyId + "").setSmallIcon(R.drawable.icon)
       .setSound(null)
       .setVibrate(null)
       .setContentTitle(title)
       .setContentText(des)
       .setAutoCancel(true)
       .setShowWhen(false)
       .setOnlyAlertOnce(true)
       .setProgress(100, downloadProgress, false);

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

    小菜测试在设置点击自动关闭属性 setAutoCancel 后,完成下载,点击通知栏消息时,该 Notification 未消失;其原因在于小菜省略了设置 setContentIntent 的 PendingIntent;即便是不需要跳转安装或其他具体页面,也需要设置一个默认的 PendingIntent;

PendingIntent pendingIntent;
if (fileType != null && fileType.equals(FileDownloadManager.FILE_TYPE_APK)) {
   pendingIntent = PendingIntent.getActivity(context, 0, clickIntent, 0);
} else {
   pendingIntent = PendingIntent.getBroadcast(context, REQUESTCODE, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);
}

NotificationCompat.Builder notification =
   new NotificationCompat.Builder(context, notifyId + "").setSmallIcon(R.drawable.icon)
       .setContentText(des)
       .setContentIntent(pendingIntent);

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

    小菜之前未尝试过滑动清除 Notification,实际与设置点击通知操作类似,也需要设置对应的 PendingIntent 到 setDeleteIntent 即可;

pendingIntent = PendingIntent.getBroadcast(context, REQUESTCODE, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);

NotificationCompat.Builder notification =
   new NotificationCompat.Builder(context, notifyId + "").setSmallIcon(R.drawable.icon)
       .setContentText(des)
       .setDeleteIntent(pendingIntent);

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


版权申明:本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!

全部评论

文明上网理性发言,请遵守新闻评论服务协议

条评论