在我的黑莓应用程序中,我正在使用备用入口点,并在启动时运行一个线程,执行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