@Override public void onReceive(Context context, Intent intent) { this.task.cancel(); } }
/** * Attempts to stop the working task, halts the processing of waiting tasks, and returns a list * of the tasks that were awaiting execution. These tasks are drained (removed) from the task * queue upon return from this method. */ public synchronized DownloadTask[] shutdown() { shutedDown = true; if (runningTask != null) runningTask.cancel(); final DownloadTask[] tasks = new DownloadTask[taskList.size()]; taskList.toArray(tasks); taskList.clear(); return tasks; }
@Override protected void onDestroy() { super.onDestroy(); if (task != null) task.cancel(); }
@Override protected void onDestroy() { super.onDestroy(); if (task != null) task.cancel(); }
@Override protected void onDestroy() { super.onDestroy(); if (task != null) task.cancel(); }
@Override public void onClick(View v) { if (v.getTag() == null) { // need to start GlobalTaskManager.getImpl().enqueueTask(task, listener); actionTv.setText(R.string.cancel); v.setTag(new Object()); } else { // need to cancel task.cancel(); } } });
/** * Pause the queue. * * @see #resume() */ public synchronized void pause() { if (paused) { Util.w(TAG, "require pause this queue(remain " + taskList.size() + "), but" + "it has already been paused"); return; } paused = true; if (runningTask != null) { runningTask.cancel(); taskList.add(0, runningTask); runningTask = null; } }
@Override public void onClick(View v) { if (actionView.getTag() == null) { // choose file if (uri == null) { final Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT); intent.addCategory(Intent.CATEGORY_OPENABLE); intent.setType("application/apk"); intent.putExtra(Intent.EXTRA_TITLE, "liulishuo.apk"); startActivityForResult(intent, WRITE_REQUEST_CODE); } else { handleUri(uri); } } else { task.cancel(); } } });
@Override public void onClick(View v) { final boolean started = task.getTag() != null; if (started) { // to cancel task.cancel(); } else { actionTv.setText(R.string.cancel); // to start startTask(statusTv, progressBar, actionTv); // mark task.setTag("mark-task-started"); } } });
@Override public void onClick(View v) { final boolean started = task.getTag() != null; if (started) { // to cancel task.cancel(); } else { actionTv.setText(R.string.cancel); // to start startTask(statusTv, actionTv, taskPb, block0Pb, block1Pb, block2Pb, block3Pb, taskTitleTv, block0TitleTv, block1TitleTv, block2TitleTv, block3TitleTv, taskSpeedTv, block0SpeedTv, block1SpeedTv, block2SpeedTv, block3SpeedTv); // mark task.setTag("mark-task-started"); } } });
@Test public void pause_isRunning_cancel() { serialQueue.runningTask = task1; serialQueue.pause(); verify(task1).cancel(); assertThat(taskList.get(0)).isEqualTo(task1); }
@Test public void shutdown() { taskList.add(task1); serialQueue.runningTask = task2; final DownloadTask[] tasks = serialQueue.shutdown(); verify(task2).cancel(); assertThat(serialQueue.shutedDown).isTrue(); assertThat(tasks).containsExactly(task1); }
@Test public void operation() { final DownloadDispatcher dispatcher = OkDownload.with().downloadDispatcher(); final String url = "url"; final Uri uri = mock(Uri.class); when(uri.getScheme()).thenReturn(ContentResolver.SCHEME_FILE); when(uri.getPath()).thenReturn("~/path"); DownloadTask task = new DownloadTask.Builder(url, uri).build(); // enqueue final DownloadListener listener = mock(DownloadListener.class); task.enqueue(listener); assertThat(task.getListener()).isEqualTo(listener); verify(dispatcher).enqueue(eq(task)); // cancel task.cancel(); verify(dispatcher).cancel(eq(task)); // execute task.execute(listener); assertThat(task.getListener()).isEqualTo(listener); verify(dispatcher).execute(eq(task)); }