在我的黑莓應用程序中,我正在使用備用入口點,並在啓動時運行一個線程,執行http操作,同一線程在一段時間後會重複調用3分鐘。它做它的操作,但問題是它掛起UI 這裏是我的代碼。黑莓服務線程在執行時掛起ui
final class sendUnsentService extends Thread {
Timer timer;
TimerTask repeatMe;
sendUnsentService me;
boolean working = false;
public boolean isWorking() {
return working;
}
public void interrupt() {
super.interrupt();
System.out.println("___________________________[STOPSERVICE()]");
try {
timer.cancel();
} catch (Exception e) {
System.out
.println("_______________________[PROBLEM STOPPING SERVICE]");
}
}
public void run() {
super.run();
System.out.println("___________________________[STARTSERVICE()]");
new Thread() {
public void run() {
timer.schedule(repeatMe, 0, 300000);
};
}.start();
}
public sendUnsentService() {
me = this;
ImageUtils.initPersistentStorage();
timer = new Timer();
repeatMe = new TimerTask() {
public void run() {
working = true;
if (sendUnsentActivity()) {
me.interrupt();
}
working = false;
}
};
}
我上一些用戶交互使用此代碼調用這個線程然後將其掛在UI
try {
helloBerry.service = new sendUnsentService();
} catch (Exception e) {
System.out.println("_____________1 " + e);
}
try {
helloBerry.service.startService();
} catch (Exception e) {
System.out.println("_____________2 " + e);
}
,這是我如何調用服務使用的替代品開始在設備啓動在主方法入口點
if (args != null && args.length > 0 && args[0].equals("normal")) {
System.out.println("_________[STARTING APP]");
___________some code here to show a screen
}
} else {
System.out.println("_________[STARTING SERVICE]");
service = new sendUnsentService();
service.run();
}
您可能不得不使用RuntimeStore將信息傳遞到後臺應用程序進行處理,或將其存儲在某個標準位置,並且當它看到它知道要開始的某些數據的存在發送。據我所知,如果用戶退出應用程序沒有什麼可以做,以防止線程被殺害。如果傳輸仍在進行中,您可以重寫主屏幕上的onClose()方法作爲後臺自身,然後在完成時自動關閉。 – jprofitt