Android multithreaded breakpoint Download

Posted by ShawnK on Sun, 30 Jan 2022 12:33:16 +0100

QDownload

QDownload is a download framework based on Android platform. API is simple and easy to use. It only takes 5 minutes to realize the function of multi task, multi thread and breakpoint download

The supported functions are as follows:

  • Support multiple downloading tasks at the same time
  • A single task supports multiple threads to download
  • Support breakpoint download. Download can be resumed when the network is disconnected and the process is crossed out
  • Automatically verify whether the file server supports breakpoint download. If not, single thread task download will be started
  • Support application global monitoring and download progress callback
  • Support download speed display
  • Support adding download tasks, suspending download, resuming download and canceling download
  • Support batch pause and batch resume download

Paste an effect picture first

homepage

Multitasking multithreading breakpoint download page

1. How to use

1.1. Import dependency

implementation 'com.qw.download:download:1.0.0-alpha01'

1.2. Initialize download component

public class MyApplication extends Application{
    @Override
    public void onCreate() {
        super.onCreate();
        //Initialize download component (can be done in sub thread)
        DownloadManager.init(this);
    }    
}

1.3. Core controller DownloadManager

The api is as follows

public class DownloadManager {
    private static DownloadManager mInstance;
    private final Context context;
    
    private DownloadManager(Context context) {}
    //Initialize component
    public static void init(Context context) {}
    //Open download
    public static void add(DownloadEntry entry) {}  
    //Pause Download
    public static void pause(DownloadEntry entry) {}    
    //Pause all tasks
    public static void pauseAll() {}
    //Resume Download
    public static void resume(DownloadEntry entry) {}   
    //Restore all tasks
    public static void recoverAll() {}
}

1.4. Monitor download progress

You need to monitor the download progress. You can register DownloadWatcher to monitor the changes of download information

private DownloadWatcher watcher = new DownloadWatcher() {
    @Override
    protected void onDataChanged(DownloadEntiry entry) {
	    //Here to monitor the real-time information downloaded
        mDownloadInfoLabel.setText(entry.toString());
        Log.e("MainActivity", entry.toString());
    }
};

@Override
protected void onResume() {
    super.onResume();
    //Registered observer
    DownloadManager.addObserver(watcher);
}

@Override
protected void onPause() {
    super.onPause();
    //Remove observer
    DownloadManager.removeObserver(watcher);
}

The third line downloadentry stores the download related information

public class DownloadEntry implements Serializable {
    public String id;//Unique identification of the download
    public String url;//url address of the download file
    public boolean isSupportRange;//Support breakpoint continuation
    public long contentLength;//file length
    public long currentLength;//Downloaded file length
    public State state;//Task status
    public HashMap<Integer, Long> ranges;//Stores the download start block for each thread
    public int speed;//Download speed unit: s
}

state has the following values

public enum State {
    IDLE,//free
    CONNECT,//Connection in progress (user prefetches file information)
    ING,//Downloading
    PAUSED,//Paused
    CANCELLED,//Cancelled
    ERROR,//error
    DONE,//complete
    WAIT//wait for
}

1.5. Download related operations

Add a download

Build download entity through DownloadEntry

public void addDownload(String id,String url){
    //id is a unique identification of the task, and url is the file address
    DownloadEntry entry = DownloadEntry.obtain(id,url)
    //Add a download task
    DownloadManager.add(entry)    
}

Pause Download

You can download the task id and then call DownloadManager.. Findbyid (id) function to get DownloadEntry

public void pauseDownload(DownloadEntry entry){
    //Add a download task
    DownloadManager.pause(entry)    
}

Resume Download

public void resumeDownload(DownloadEntry entry){
    //Add a download task
    DownloadManager.resume(entry)    
}

Pause all download tasks

public void pauseAll(DownloadEntry entry){
    //Add a download task
    DownloadManager.resume(entry)    
}

Resume all download tasks

public void recoverAll(DownloadEntry entry){
    //Add a download task
    DownloadManager.recoverAll(entry)    
}

1.6. A scenario of apk download in the application market

Apkientry entity data is used to describe the basic information of apk

public class ApkEntry {
    public String id;//Package id
    public String cover;//apk Icon
    public String name;//apk name
    public String url;//apk download address
    public long length;//apk size

    public String id() {
        if (TextUtils.isEmpty(id)) {
            //If the server does not return a unique tag, the md5 value of the url is used as the unique ID of the download
            id = FileUtilities.getMd5FileName(url);
        }
        return id;
    }
}

I want to download this apk, so I can do it

public void downloadApk(ApkEntry apkEntry){
    //1. check whether the apk is currently downloading
    DownloadEntry entry = DownloadManager.findById(apkEntry.id())
    if(entry==null || entry.state==State.PAUSED || entry.state==State.ERROR || entry.state==State.CANCELLED){
        //There are four situations: no download task 𞓜 the task is suspended | the download fails | the task has been cancelled
        DownloadManager.add(entry)
    }
}

Pause apk Download

public void pauseDownloadApk(ApkEntry apkEntry){
    //1. check whether the current apk is in the download task first
    DownloadEntry entry = DownloadManager.findById(apkEntry.id())
    if(entry!=null){
        DownloadManager.pause(entry)
    }
}

For more functions, please refer to the implementation in the demo

If you encounter problems during docking, you can also + me QQ: 435231045

Topics: Java Android Apache