在我的下载管理器中,当活动暂停或停止时,我会自动暂停下载。任何正在运行的下载都会中断。我知道我应该让它在后台下载,但这不是重点。
问题是,在恢复时,下载必须继续。但是,当我在 logcat 中放置一个日志时,我得到一个线程已经在运行的异常,该日志清楚地表明线程被中断。
指向的行Scheduler
是负责运行线程的类:
public class Scheduler extends TimerTask {
private static int index = 0;
//------------------------------------------------------------------------------
@Override
public void run() {
if(Sharable.isResumed()){ // activity is ready for interaction with user
if(Sharable.getShouldLook()){ // and we are ready to download
while(index < Sharable.downloads.size()){
Task t = Sharable.downloads.get(index++);
Log.v("SCHEDULER", "CHECKING");
if(t.getReadiness()==true && t.getProgress() != 100){
Log.v("TASK-- ", t.toString() + " is not running. Now starting");
if(!t.isAlive()){ // check if not already running
t.start(); // start the thread
}
break;
}
}
index = 0;
}
}
}
//------------------------------------------------------------------------------
}
t.start()
是罪魁祸首。为什么?我不知道为什么。
那么 logcat 中有什么?
08-21 15:50:54.316: V/TASKPATH(1460): /sdcard/tqa-se3ep47.mp3
08-21 15:50:54.386: V/TASK(1460): SETTING PATH
08-21 15:50:54.437: V/TASK(1460): MAKING URL
08-21 15:50:54.437: V/TASK(1460): MAKING CONNECTION
08-21 15:50:54.466: V/TASK(1460): Range:bytes=16636099-
08-21 15:50:54.466: V/TASK(1460): BYTES-WRITTEN: 16636099
08-21 15:50:54.514: V/TASK(1460): CONTENT-LENGTH: 27452333
08-21 15:50:55.326: V/TASK(1460): CONNECTED
08-21 15:50:56.886: V/TASK(1460): CODE: 206
08-21 15:50:56.886: V/TASK(1460): Partial Content
08-21 15:50:56.896: V/TASK(1460): Writing at location 16636099
.... some useless logs skipped for sake of sanity ....
08-21 15:51:00.986: V/TASK(1460): Writing at location 16763075
08-21 15:51:01.036: V/TASK(1460): 61.0
08-21 15:51:01.046: V/TASK(1460): Writing at location 16765123
08-21 15:51:01.056: V/TASK(1460): 61.0
08-21 15:51:01.196: V/TASK(1460): Writing at location 16767171
08-21 15:51:01.196: V/TASK(1460): 61.0
08-21 15:51:01.246: V/TASK(1460): Writing at location 16769219
08-21 15:51:01.246: V/TASK(1460): 61.0
08-21 15:51:01.266: V/TASK(1460): Writing at location 16771267
08-21 15:51:01.286: V/TASK(1460): 61.0
08-21 15:51:01.426: V/TASK(1460): Writing at location 16773315
08-21 15:51:01.437: V/TASK(1460): 61.0
08-21 15:51:01.486: V/TASK(1460): Writing at location 16775363
08-21 15:51:01.516: V/TASK(1460): 61.0
08-21 15:51:01.546: V/TASK(1460): Writing at location 16777411
08-21 15:51:01.546: V/TASK(1460): 61.0
08-21 15:51:01.586: V/TASK(1460): Writing at location 16779459
08-21 15:51:01.586: V/TASK(1460): 61.0
08-21 15:51:01.616: V/TASK(1460): Writing at location 16781507
08-21 15:51:01.616: V/TASK(1460): 61.0
08-21 15:51:01.676: V/TASK(1460): Writing at location 16783555
08-21 15:51:01.887: V/TASK(1460): 61.0
08-21 15:51:02.106: V/TASK(1460): INTERRUPTED
08-21 15:51:12.319: V/TASK--(1460): tqa-se3ep47.mp3 is not running. Now starting
最后一行显示调度程序正在启动线程。它将以与日志显示的相同方式启动。设置路径、URL 等。这就是onResume()
. 如果我要关闭并重新启动应用程序,一切都会好起来的。
错误日志:
08-21 15:51:12.355: E/AndroidRuntime(1460): FATAL EXCEPTION: Scheduler
08-21 15:51:12.355: E/AndroidRuntime(1460): java.lang.IllegalThreadStateException: Thread already started.
08-21 15:51:12.355: E/AndroidRuntime(1460): at java.lang.Thread.start(Thread.java:1045)
08-21 15:51:12.355: E/AndroidRuntime(1460): at com.example.simpledownloader.scheduler.Scheduler.run(Scheduler.java:23)
08-21 15:51:12.355: E/AndroidRuntime(1460): at java.util.Timer$TimerImpl.run(Timer.java:284)