通过在service的相关函数中打印线程ID,代码如下:
class ProgressBarService extends Service
@Override
protected void ready() { super.ready(); System.out.println("ProgressBarService ready!thread id="+Thread.currentThread().getId()); }
@Override
protected void scheduled() { super.scheduled(); System.out.println("ProgressBarService scheduled!thread id="+Thread.currentThread().getId()); }
@Override
protected void running() { super.running(); System.out.println("ProgressBarService running!thread id="+Thread.currentThread().getId()); }
@Override
protected void succeeded() { super.succeeded(); System.out.println("ProgressBarService succeeded!"); }
@Override
protected void cancelled() { super.cancelled(); System.out.println("ProgressBarService cancelled!thread id="+Thread.currentThread().getId()); }
@Override
protected void failed() { super.failed(); System.out.println("ProgressBarService failed!"); }
@Override
public boolean cancel() {
System._out_.println("ProgressBarService cancel!thread id="+Thread._currentThread_().getId());
return super.cancel(); }
@Override
public void restart() { super.restart(); System.out.println("ProgressBarService restart!"); }
@Override
public void reset() { super.reset(); System.out.println("ProgressBarService reset!"); }
@Override
public void start() { super.start(); System.out.println("ProgressBarService start! thread id="+Thread.currentThread().getId()); }
@Override
protected void executeTask(Task
@Override
protected Task
@Override
protected void running() { super.running(); System.out.println("createTask running.....thread id="+Thread.currentThread().getId()); }
@Override
protected void succeeded() { super.succeeded(); System.out.println("createTask succeeded.....thread id="+Thread.currentThread().getId()); }
@Override
protected void cancelled() { super.cancelled(); System.out.println("createTask cancelled.....thread id="+Thread.currentThread().getId()); }
@Override
protected void failed() { super.failed(); System.out.println("createTask failed.....thread id="+Thread.currentThread().getId()); }
@Override
protected void updateProgress(long workDone, long max) { super.updateProgress(workDone, max); System.out.println("createTask long updateProgress.....thread id="+Thread.currentThread().getId()); }
@Override
protected void updateProgress(double workDone, double max) { super.updateProgress(workDone, max); System.out.println("createTask double updateProgress....."); }
@Override
protected void updateMessage(String message) { super.updateMessage(message); System.out.println("createTask updateMessage.....thread id="+Thread.currentThread().getId()); }
@Override
protected void updateTitle(String title) { super.updateTitle(title); System.out.println("createTask updateTitle.....thread id="+Thread.currentThread().getId()); }
@Override
protected void updateValue(Integer value) { super.updateValue(value); System.out.println("createTask updatevalue....thread id="+Thread.currentThread().getId()); }
@Override
protected Integer call() throws Exception { for(int i=0;i<100;i++) { if(isCancelled()) { System.out.println("createTask call iscancelled!"); return -1; } System.out.println("createTask call thread id="+Thread.currentThread().getId()); Thread.sleep(200); updateProgress(i,100); } updateProgress(100,100); return 100; } }; } }
点击cancel按钮,出现如下显示:
main thread id=18
ProgressBarService createTask!
ProgressBarService scheduled!thread id=18
createTask scheduled.....thread id=18
ProgressBarService executeTask!
ProgressBarService start! thread id=18
createTask call thread id=43
createTask scheduled.....thread id=18
ProgressBarService running!thread id=18
createTask running.....thread id=18
createTask double updateProgress.....
createTask long updateProgress.....thread id=43
createTask call thread id=43
createTask double updateProgress.....
createTask long updateProgress.....thread id=43
createTask call thread id=43
createTask double updateProgress.....
createTask long updateProgress.....thread id=43
createTask call thread id=43
createTask double updateProgress.....
createTask long updateProgress.....thread id=43
createTask call thread id=43
createTask double updateProgress.....
createTask long updateProgress.....thread id=43
createTask call thread id=43
createTask double updateProgress.....
createTask long updateProgress.....thread id=43
createTask call thread id=43
createTask double updateProgress.....
createTask long updateProgress.....thread id=43
createTask call thread id=43
createTask double updateProgress.....
createTask long updateProgress.....thread id=43
createTask call thread id=43
createTask double updateProgress.....
createTask long updateProgress.....thread id=43
createTask call thread id=43
createTask double updateProgress.....
createTask long updateProgress.....thread id=43
createTask call thread id=43
createTask double updateProgress.....
createTask long updateProgress.....thread id=43
createTask call thread id=43
createTask double updateProgress.....
createTask long updateProgress.....thread id=43
createTask call thread id=43
ProgressBarService cancel!thread id=18
ProgressBarService cancelled!thread id=18
createTask cancelled.....thread id=18 这个地方可以直接操作GUI的控件了(后来我在此函数中增加操作GUI控件的代码实现OK)
如果100%执行完成,则还发现:
ProgressBarService succeeded!
createTask succeeded.....thread id=18 这也说明createTask中的task当执行完成之后调用succeeded()时也是在主线程中,当然可以操作各种控件了。