?? backgroundtask.java
字號(hào):
/*
* Created on 2005-2-26
*
* 進(jìn)度條與線程模型
*/
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 Favo
*
* 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()) {//如果用戶(hù)中斷了程序
if (needAlert) {
display.setCurrent(alertScreen, preScreen);
} else {
display.setCurrent(preScreen);
}
}
}
poUI.exit();
}
}
/**
* 須由用戶(hù)定義的任務(wù)
* 注意!!!
* 任務(wù)如果成功的運(yùn)行,應(yīng)該由此方法內(nèi)部負(fù)責(zé)跳轉(zhuǎn)至成功畫(huà)面,并返回true.
* 若任務(wù)運(yùn)行失敗,請(qǐng)?jiān)O(shè)置needAlert(是否需要警報(bào)),AlertScreen(警報(bào)畫(huà)面),preScreen(跳轉(zhuǎn)回的前一個(gè)具體屏幕)
* 手動(dòng)更新進(jìn)度欄,請(qǐng)調(diào)用pgUI.updateProgress().
* 請(qǐng)確保當(dāng)cancel()調(diào)用時(shí),此方法會(huì)立即退出,并返回false(如果因?yàn)楫惓L龃撕瘮?shù)是可以接受的行為).
*/
public abstract boolean runTask();
/**
* 這是一個(gè)偷懶的辦法,當(dāng)你構(gòu)造好BackgroundTask對(duì)象后,直接調(diào)用這個(gè)方法, 可以幫助你初始化進(jìn)度UI,并顯示出來(lái)。之后啟動(dòng)你的任務(wù)線程
*/
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方法,因?yàn)閞unTask已經(jīng)有了返回值
//
// public void taskComplete(){
// getProgressObserver().setStopped(false);
// }
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -