在 try/catch Thread.sleep() 之前,Toast 不会显示
当我尝试 show();
我的 Toast 时,我遇到了一个奇怪的问题。您将在下面看到两个由 try/catch 和 Thread.sleep();
分隔的 Toast;在这种情况下,将显示第二个 Toast,toast2
,但 toast1 不会。
如果我删除 try/catch
两个 Toast 将毫无问题地显示。
我在 SO 的其他地方看到 toast.show();
在 UI 线程上发出请求,该请求可能会与其他操作发生冲突。我想知道这是否与我在 Thread.sleep(); 中遇到的问题相同 我该如何解决这个问题?
谢谢
TestService.java
///Debug - Show a Toast
// Toast does NOT show up
Toast toast1 = Toast.makeText(context,"Service Started", Toast.LENGTH_SHORT);
toast1.show();
//Try to sleep for roughly 2 seconds
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//Debug - Shows a Toast
Toast toast2 = Toast.makeText(context,"Sleep completed", Toast.LENGTH_SHORT);
toast2.show();
I've got a curious problem when I try to show();
my Toasts. You'll see below two Toasts separated by a try/catch and Thread.sleep();
In this case the second Toast, toast2
, will show up but toast1
will not.
If I remove the try/catch
both Toasts will show up with no problem.
I've seen elsewhere on SO that toast.show();
makes a request on the UI Thread which can be conflicted by other operations. I'm wondering if that is the same problem I have here with the Thread.sleep();
How can I solve this problem?
Thank you
TestService.java
///Debug - Show a Toast
// Toast does NOT show up
Toast toast1 = Toast.makeText(context,"Service Started", Toast.LENGTH_SHORT);
toast1.show();
//Try to sleep for roughly 2 seconds
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//Debug - Shows a Toast
Toast toast2 = Toast.makeText(context,"Sleep completed", Toast.LENGTH_SHORT);
toast2.show();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我遇到了同样的问题,我将尝试按照建议使用 AsyncTask 来解决,但我的用例如下:
1)我想显示 Toast 消息(开始广播消息)
2)如果条件合适,我向客户端广播message1。
3) 我睡了 2 秒以便广播完成。
4)我一直向客户端广播message2。
5)如果完成了,我会Toast另一条消息(广播消息已完成)
,我想出了以下解决方法示例(尽管Toast消息的顺序仍然以相反的方式显示):
I faced the same issue, I will attempt to resolve by using AsyncTask as suggested, but my use-case is the following:
1) I want to display a Toast message (begin broadcast message)
2) If the condition is right, I broadcast message1 to clients.
3) I sleep 2 secs to allow broadcasting to complete.
4) I broadcast message2 to clients all the time.
5) If it's done I would Toast another message (broadcasting messages completed)
I've come up with the following workaround example (though the sequence the toast messages were still shown in reverse):
在现实生活中,你可能会有一些逻辑而不仅仅是“睡觉”,对吧?
根据您的示例,启动服务的正确 android 方法是在工作线程上执行它。这将确保您不会收到 ANR。
你的可能看起来像:
In real life, you will probably have some logic instead of just "sleep", right?
THe right android way of, per your example, starting a service would be through executing it on a worker thread. This will make sure you won't get an ANR.
your could would look something like:
由于您在第一次 toast 之后立即挂起 ui 线程,因此它不会
有机会展示。当睡眠结束时,就是第一次的时间
敬酒已经过去了。
这听起来是最合理的解释,但我可能是错的。需要深入挖掘 Android
代码来确定。
Since you suspend ui thread right after first toast it does not
have chance to be shown. And by the time sleep is over, the time for first
toast has passed.
That sounds like most plausible explanation but I may be wrong. Need to dig really deep in Android
code to find out for sure.
我假设您需要创建一个持续超过 2 秒的 Toast(为什么您应该在发送另一个 Toast 之前使用 Thread.Sleep(2000) ?)。
所以我刚刚发现此链接提供了一个非常很好的例子。
希望这有帮助
I'm assuming you need to create a Toast that lasts more than 2sec (why you should use
Thread.Sleep(2000)
before sending another Toast?).So I've just found this link that provides a very good example.
Hope this helps