Giter VIP home page Giter VIP logo

awesomedownloader-android's Introduction

AwesomeDownloader

介绍

AwesomeDownloader 是基于OkHttp和kotlin协程实现的下载器,它能在后台进行下载任务并轻松地让您在下载文件时获取进度,它能随时停止、恢复、取消任务,还可以方便地查询下载的任务和已完成的任务的信息。

功能&特性

下载文件

监听下载

断点续传

随时控制下载

查询下载任务 (支持返回LiveData)

可通过通知栏显示下载情况

下载多媒体文件加入多媒体库

自动/手动清除缓存文件

支持链式调用

导入依赖

  1. 把它添加到你的根目录build.gradle中,在repositories的最后:
	allprojects {
		repositories {
			...
			maven { url 'https://jitpack.io' }
		}
	}
  1. 添加依赖: version
	dependencies {
	        implementation 'com.gitee.jiang_li_jie_j:awesome-downloader:v1.2.2-alpha'
	}

使用说明

1.申请读写权限,网络权限

2.初始化下载器,传入Application的context

kotlin:

    //默认模式启动(与页面绑定,页面销毁时,下载器也会结束生命)传入FragmentActivity或Fragment
    AwesomeDownloader.initWithDefaultMode(requireActivity())

    //前台服务模式启动(独立启动,直至服务被kill或关闭)传入能创建服务的ContextWrapper
    AwesomeDownloader.initWithServiceMode(this)

java:

	//默认模式启动(与页面绑定,页面销毁时,下载器也会结束生命)传入FragmentActivity或Fragment
	AwesomeDownloader.INSTANCE.initWithDefaultMode(this);

	//前台服务模式启动(独立启动,直至服务被kill或关闭)传入能创建服务的ContextWrapper
	AwesomeDownloader.INSTANCE.initWithServiceMode(this);

3.下载文件

kotlin:

    	val url = "https://images.gitee.com/uploads/images/2020/0919/155031_538a3406_5577115.png"
       //获取应用私有照片储存路径
       val filePath = PathSelector(applicationContext).getPicturesDirPath()
       //加入下载队列
       AwesomeDownloader.enqueue(url,filePath,"test.png")

java:

    String url = "https://images.gitee.com/uploads/images/2020/0919/155031_538a3406_5577115.png";
	//获取应用私有照片储存路径
    String filePath = new PathSelector(applicationContext).getPicturesDirPath();
	//加入下载队列
    AwesomeDownloader.INSTANCE.enqueue(url, filePath, "test.png");

4.下载控制

kotlin:

        //停止全部
        AwesomeDownloader.stopAll()
        //恢复下载
        AwesomeDownloader.resume()
        //取消当前
        AwesomeDownloader.cancel()
        //取消全部
        AwesomeDownloader.cancelAll()

5.添加监听&移除监听

kotlin:

        //添加监听
        AwesomeDownloader.addOnProgressChangeListener{ progress ->
            //do something...
        }.addOnStopListener{ downloadBytes, totalBytes ->
            //do something...
        }.addOnFinishedListener{ filePath, fileName ->
            //do something...
        }.addOnErrorListener{ exception ->
            //do something...
        }
        
         //移除全部进度监听
         AwesomeDownloader.removeAllOnProgressChangeListener()
         
         //移除最后一个进度监听
         AwesomeDownloader.onDownloadProgressChange.removeLast()
         
         //移除最前的进度监听
         AwesomeDownloader.onDownloadProgressChange.removeFirst()

java:

        //添加监听
        AwesomeDownloader.INSTANCE
        .addOnProgressChangeListener(progress -> {
            //do something
            return null;
        }).addOnStopListener((downloadBytes, totalBytes) -> {
            //do something
            return null;
        }).addOnFinishedListener((filePath, fileName) -> {
            //do something
            return null;
        }).addOnErrorListener(exception -> {
            //do something
            return null;
        });
        
         //移除全部进度监听
         AwesomeDownloader.INSTANCE.removeAllOnProgressChangeListener();
         
         //移除最后一个进度监听
         AwesomeDownloader.INSTANCE.getOnDownloadProgressChange()
         .removeLast();
         
         //移除最前的进度监听
         AwesomeDownloader.INSTANCE.getOnDownloadProgressChange()
         .removeFirst();

6.设置自定义通知栏

(默认显示的通知栏)

默认显示的通知栏

设置中确保showNotification为true

 AwesomeDownloader.option.showNotification = true

调用setNotificationSender()

override 抽象类NotificationSender 的三个方法

kotlin:

 AwesomeDownloader.setNotificationSender(object : NotificationSender(applicationContext) {
                //创建显示任务下载进度的Notification
                override fun buildDownloadProgressNotification(
                    progress: Int,
                    fileName: String
                ): Notification {
                    return NotificationCompat.Builder(context, CHANNEL_ID)
                        .setSmallIcon(R.drawable.ic_baseline_adb_24)
                        .setContentTitle("$fileName 下载中")
                        .setContentText("$progress%")
                        .setPriority(NotificationCompat.PRIORITY_HIGH)
                        .build()
                }

                //创建显示任务下载停止的Notification
                override fun buildDownloadStopNotification(fileName: String): Notification {
                    return NotificationCompat.Builder(context, CHANNEL_ID)
                        .setSmallIcon(R.drawable.ic_baseline_adb_24)
                        .setContentTitle("$fileName Stop")
                        .setContentText("Stop")
                        .setPriority(NotificationCompat.PRIORITY_HIGH)
                        .build()
                }

                //创建显示任务下载完成的Notification
                override fun buildDownloadDoneNotification(
                    filePath: String,
                    fileName: String
                ): Notification {
                    Log.d(TAG, "buildDownloadDoneNotification: start")
                    return if (isImageFile(fileName)) {
                        val bitmap =
                            BitmapFactory.decodeFile("$filePath/$fileName")
                        Log.d(TAG, "buildDownloadDoneNotification: done")
                        NotificationCompat.Builder(context, CHANNEL_ID)
                            .setSmallIcon(R.drawable.ic_baseline_adb_24)
                            .setContentTitle("$fileName Done")
                            .setContentText("Done")
                            .setStyle(
                                NotificationCompat.BigPictureStyle()
                                    .bigPicture(bitmap)
                                    .bigLargeIcon(null)
                            )
                            .setPriority(NotificationCompat.PRIORITY_HIGH)
                            .build()

                    } else {
                        NotificationCompat.Builder(context, CHANNEL_ID)
                            .setSmallIcon(R.drawable.ic_baseline_adb_24)
                            .setContentTitle("$fileName Done")
                            .setContentText("Done")
                            .setPriority(NotificationCompat.PRIORITY_HIGH)
                            .build()
                    }
                }
            })

(通过setNotificationSender()设置的通知栏)

自定义显示的通知栏

(通知栏效果可能因为Android版本不同和手机厂商不同而效果不一致)

7.查询下载任务

kotlin:

        lifecycleScope.launch(Dispatchers.IO) {
            //获取全部任务信息
            AwesomeDownloader.queryAllTaskInfo()
            //获取完成的任务信息
            AwesomeDownloader.queryFinishedTaskInfo()
            //获取完成的任务信息
            AwesomeDownloader.queryUnfinishedTaskInfo()
          	//根据id删除数据库中的任务记录
            AwesomeDownloader.deleteById(id)
        }

		//获取当前下载中的任务
		AwesomeDownloader.getDownloadingTask()

		//获取队列中的任务
		AwesomeDownloader.getDownloadQueueArray()

java:

	//获取全部任务信息
	AwesomeDownloader.INSTANCE.getAllTaskInfoLiveData().getValue();
		
	//获取完成的任务信息
	AwesomeDownloader.INSTANCE.getFinishedTaskInfoLiveData().getValue();

	//获取完成的任务信息
	AwesomeDownloader.INSTANCE.getUnfinishedTaskInfoLiveData().getValue();

	//获取当前下载中的任务
	AwesomeDownloader.INSTANCE.getDownloadingTask();

	//获取队列中的任务
	AwesomeDownloader.INSTANCE.getDownloadQueueArray();

awesomedownloader-android's People

Contributors

amancalljiang avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

awesomedownloader-android's Issues

Android 12兼容性问题

Caused by: java.lang.IllegalArgumentException: com.shopee.aabinstall: Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent.
Strongly consider using FLAG_IMMUTABLE, only use FLAG_MUTABLE if some functionality depends on the PendingIntent being mutable, e.g. if it needs to be used with inline replies or bubbles.
at android.app.PendingIntent.checkFlags(PendingIntent.java:378)
at android.app.PendingIntent.getBroadcastAsUser(PendingIntent.java:648)
at android.app.PendingIntent.getBroadcast(PendingIntent.java:635)
at com.jiang.awesomedownloader.core.sender.DefaultNotificationSender.createPendingIntent(DefaultNotificationSender.kt:46)
at com.jiang.awesomedownloader.core.sender.DefaultNotificationSender.(DefaultNotificationSender.kt:29)
at com.jiang.awesomedownloader.core.AwesomeDownloader.initSender(AwesomeDownloader.kt:72)
at com.jiang.awesomedownloader.core.AwesomeDownloader.initWithDefaultMode(AwesomeDownloader.kt:83)

在Target APi为31时,编译会有这个问题

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.