Skip to content

Advanced Use Guideline

Jacksgong edited this page Apr 5, 2018 · 15 revisions

Debug OkDownload

I. Global Control

OkDownload.with().setMonitor(monitor);

DownloadDispatcher.setMaxParallelRunningCount(3);

RemitStoreOnSQLite.setRemitToDBDelayMillis(3000);

OkDownload.with().downloadDispatcher().cancelAll();

II. Injection Component

If you want to inject your components, please invoke following method before you using OkDownload:

OkDownload.Builder builder = new OkDownload.Builder(context)
        .downloadStore(downloadStore)
        .callbackDispatcher(callbackDispatcher)
        .downloadDispatcher(downloadDispatcher)
        .connectionFactory(connectionFactory)
        .outputStreamFactory(outputStreamFactory)
        .downloadStrategy(downloadStrategy)
        .processFileStrategy(processFileStrategy)
        .monitor(monitor);

OkDownload.setSingletonInstance(builder.build());

III. Dynamic Serial Queue

DownloadSerialQueue serialQueue = new DownloadSerialQueue(commonListener);

serialQueue.enqueue(task1);
serialQueue.enqueue(task2);

serialQueue.pause();

serialQueue.resume();

int workingTaskId = serialQueue.getWorkingTaskId();
int waitingTaskCount = serialQueue.getWaitingTaskCount();

DownloadTask[] discardTasks = serialQueue.shutdown();

IV. Combine Several DownloadListeners

DownloadListener listener1 = new DownloadListener1();
DownloadListener listener2 = new DownloadListener2();

DownloadListener combinedListener = new DownloadListenerBunch.Builder()
                   .append(listener1)
                   .append(listener2)
                   .build();

DownloadTask task = new DownloadTask.build(url, file).build();
task.enqueue(combinedListener);

V. Dynamic Change Listener For tasks

// all attach or detach is based on the id of Task in fact. 
UnifiedListenerManager manager = new UnifiedListenerManager();

DownloadListener listener1 = new DownloadListener1();
DownloadListener listener2 = new DownloadListener2();
DownloadListener listener3 = new DownloadListener3();
DownloadListener listener4 = new DownloadListener4();

DownloadTask task = new DownloadTask.build(url, file).build();

manager.attachListener(task, listener1);
manager.attachListener(task, listener2);

manager.detachListener(task, listener2);

// all listeners added for this task will be removed when task is end.
manager.addAutoRemoveListenersWhenTaskEnd(task.getId());

manager.enqueueTaskWithUnifiedListener(task, listener3);

manager.attachListener(task, listener4);
Clone this wiki locally