黑莓服务线程执行时挂起用户界面
在我的黑莓应用程序中,我使用备用入口点并在启动时运行一个线程,该线程执行 http 操作,并且在一段时间(假设 3 分钟)后重复调用同一线程。它执行其操作,但问题是它挂起用户界面 这是我的代码。
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();
}
In my blackberry app i am using alternate entry point and run a thread on startup which does http operation and the same thread is called repeatedly after some time lets say 3 minutes. It does its operation but the problem is it hangs the ui
here is my code.
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;
}
};
}
I am invoking this thread on some user interaction using this code and then it hangs the 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);
}
and this is how i call the service to start on device booting in main method using alternate entry point
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();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我注意到的第一件事是您正在从另一个线程启动 TimerTask。定时器包含自己的线程,因此这是不必要的——直接调用它即可。另外,我认为您不需要将其扩展为线程。您可能想要做的是使用 TimerTask 重新安排自身(如果它没有错误),否则就让它停止。
至于锁定 UI,除非您在 RuntimeStore 上进行一些奇怪的同步,否则这不会影响您的主程序。它应该作为两个独立的进程运行,彼此不知情。什么情况下会冻结?
First thing I notice is that you're starting a TimerTask from another Thread. A Timer contains its own Thread, so this is unnecessary -- just call it directly. Also, I don't think you need to make this extend a Thread. What you might want to do is use the TimerTask to reschedule itself if it doesn't error, otherwise just let it stop.
As far as locking up the UI, unless you're doing some odd synchronizing on a RuntimeStore, this shouldn't affect your main program. It should be running as two separate processes, unaware of each other. What circumstances does it freeze under?
我的天啊!最后我发现了这个东西,它有点愚蠢,问题是一些 sysout 语句,我用它来打印我发送到服务器的数据,谢谢 jprofit 的兴趣,
如果你能回答我,我还有另一个与此相关的问题在某些事件上启动此线程,因此当用户关闭应用程序时,它也会关闭此线程,我尝试扩展应用程序而不是线程,但我无法远程处理它。
OMG! Finally i found the thing and its kind of stupid the problem was some sysout statements which i was using to print the data which i was sending to server thank you jprofit for taking interest
i have got another question related to this if you can answer i am starting this thread on some event so when user closes the application it also close this thread i have tried extending application instead of thread but i an not able to handle it remotely.