📄 backgroundtask.java
字号:
/*
* Created on 2005-2-26
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package com.favo.ui;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Alert;
/**
* @author liyong
*
* TODO To change the template for this generated type comment go to Window -
* Preferences - Java - Code Style - Code Templates
*/
public abstract class BackgroundTask extends Thread implements Cancelable {
ProgressObserver poUI;
protected Displayable preScreen;
protected boolean needAlert;
protected Alert alertScreen;
private Display display;
/*
*
*/
public BackgroundTask(ProgressObserver poUI, Displayable pre,
Display display) {
this.poUI = poUI;
this.preScreen = pre;
this.display = display;
this.needAlert = false;
}
/*
* (non-Javadoc)
*
* @see java.lang.Thread#run()
*/
public void run() {
boolean taskComplete=false;
try {
taskComplete=runTask();
} catch (Exception e) {
Alert al = new Alert("undefine exception", e.getMessage(), null,
AlertType.ALARM);
al.setTimeout(Alert.FOREVER);
display.setCurrent(al);
} finally {
if (!taskComplete&&poUI.isStoppable()) {
if (poUI.isStopped()) {//如果用户中断了程序
if (needAlert) {
display.setCurrent(alertScreen, preScreen);
} else {
display.setCurrent(preScreen);
}
}
}
poUI.exit();
}
}
/**
* 须由用户定义的任务
* 注意!!!
* 任务如果成功的运行,应该由此方法内部负责跳转至成功画面,并返回true.
* 若任务运行失败,请设置needAlert(是否需要警报),AlertScreen(警报画面),preScreen(跳转回的前一个具体屏幕)
* 手动更新进度栏,请调用pgUI.updateProgress().
* 请确保当cancel()调用时,此方法会立即退出,并返回false(如果因为异常跳出此函数是可以接受的行为).
*/
public abstract boolean runTask();
/**
* 这是一个偷懒的办法,当你构造好BackgroundTask对象后,直接调用这个方法, 可以帮助你初始化进度UI,并显示出来。之后启动你的任务线程
*/
public static void runWithProgressGauge(BackgroundTask btask, String title,
String prompt, boolean stoppable, Display display) {
ProgressObserver po = btask.getProgressObserver();
po.reset();
po.setStoppable(stoppable);
if(stoppable){
po.setCancelalbeObject(btask);
}
po.setTitle(title);
po.setPrompt(prompt);
po.show(display);
btask.start();
}
public ProgressObserver getProgressObserver() {
return poUI;
}
//取消了taskComplete方法,因为runTask已经有了返回值
//
// public void taskComplete(){
// getProgressObserver().setStopped(false);
// }
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -