Android 事件计时(Toast 和 Intents)
我正在尝试显式启动新活动的意图,但我希望当前活动中的一些代码首先完成执行。我做了一些研究,并且有一些想法来实现这一目标,但我认为“必须有一种更简单的方法来做到这一点”。这是相关的代码块:
cpuToast(dmg);
if (player_.getStatus() == false)
{
playerWon_ = false;
Intent intent = new Intent(Main.this, Death.class);
startActivity(intent);
}
dmg 是一个 int。 cpuToast 只是创建一个字符串来显示损坏情况,然后调用 show()。 getStatus() 返回玩家是否被杀死。如果玩家被杀,我会启动一个新的意图,它将播放玩家死亡的动画。不幸的是,“死亡”活动在 Toast 变得可见之前就启动了,然后它在我不希望的死亡活动期间变得可见。
有谁知道一种简单的方法来确保 Toast 在启动死亡活动之前完全执行?从我发现的情况来看,我似乎必须创建一个“Timer”对象,而实际上我想要的只是一个简单的 while 循环,如“while(Toast.isVisable) {}”来将执行绑定到几个秒。
I'm trying to explicitly launch an intent to a new Activity, but I would like some code in the current Activity to finish executing first. I've done a bit of research, and have a couple of ideas to accomplish this but am thinking "there's gotta be an easier way to do this". Here is the relevant block of code:
cpuToast(dmg);
if (player_.getStatus() == false)
{
playerWon_ = false;
Intent intent = new Intent(Main.this, Death.class);
startActivity(intent);
}
dmg is an int. cpuToast simply makes a String to display the damage and then calls show(). getStatus() returns whether or not the player was killed. In the event that player was killed, I launch a new intent which will play an animation of the players death. Unfortunately the "Death" Activity is being launched before the Toast even becomes visible, and then it becomes visible during the Death Activity which I do not want.
Does anyone know a simple way to ensure that the Toast executes fully prior to launching the Death Activity? From what I've found it looks like I'll have to create a "Timer" object when really all I want is a simple while loop like "while(Toast.isVisable) {}" to tie up the execution for a couple of seconds.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
可以在 http 中找到一些用于更新 UI 以响应定时事件的示例代码://developer.android.com/resources/articles/timed-ui-updates.html。
Some example code for updating the UI in response to timed events can be found at http://developer.android.com/resources/articles/timed-ui-updates.html.
当您开始祝酒时,您还可以向当前活动发布一条延迟消息;该延迟消息的可运行对象可以启动新的意图。
At the time you begin your toast, you can also post a delayed message to your current activity; the runnable of that delayed message can start the new intent.
一定要吐司吗?
如果您愿意使用自定义对话框(删除标题、按钮等),那么这可能是您最好的选择。使用处理程序在给定时间后关闭对话框并启动新活动。
Does it have to be a toast?
If you are up for the idea of using a custom dialog (removed title, buttons etc.) that is probably your best bet. Use a handler to dismiss the dialog after given time and start the new activity.