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

简介: Android 重定向文件下载尝试与通知栏信息提醒!

    小菜之前在 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 未消失;其原因在于小菜省略了设置 setContentIntentPendingIntent;即便是不需要跳转安装或其他具体页面,也需要设置一个默认的 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,实际与设置点击通知操作类似,也需要设置对应的 PendingIntentsetDeleteIntent 即可;

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);

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

来源: 阿策小和尚
目录
相关文章
|
2月前
|
Android开发
安卓SO层开发 -- 编译指定平台的SO文件
安卓SO层开发 -- 编译指定平台的SO文件
30 0
|
4月前
|
开发工具 Android开发 开发者
Android Studio详细下载,安装使用教程
Android Studio详细下载,安装使用教程
297 0
|
4月前
|
XML Java Android开发
Android App开发网络通信中使用okhttp下载和上传图片、文件讲解及实战(超详细实现用户注册信息上传 附源码)
Android App开发网络通信中使用okhttp下载和上传图片、文件讲解及实战(超详细实现用户注册信息上传 附源码)
131 0
|
1月前
|
Shell 开发工具 Android开发
ADB 下载、安装及使用教程:让你更好地管理 Android 设备
ADB 下载、安装及使用教程:让你更好地管理 Android 设备
453 2
|
1月前
|
Android开发 对象存储
OSS对象储存android开发进行下载到本地文件时异步操作失效
android vivo80使用官方示例代码进行文件下载,但是使用oss.asyncGetObject(get, new OSSCompletedCallback<GetObjectRequest, GetObjectResult>()时onSuccess和onFailure不执行
|
2月前
|
JSON Java Go
|
2月前
|
算法 Java Android开发
安卓逆向 -- 调用其他APK的SO文件
安卓逆向 -- 调用其他APK的SO文件
17 0
|
2月前
|
Android开发
安卓逆向 -- Hook多个dex文件
安卓逆向 -- Hook多个dex文件
18 1
|
3月前
|
IDE 开发工具 Android开发
Android Studio 下发布项目成APK文件
Android Studio 下发布项目成APK文件
117 1
|
3月前
|
安全 Android开发 数据安全/隐私保护
安卓逆向 -- SO文件逆向分析
安卓逆向 -- SO文件逆向分析
19 0