如何在任何循环中使用 'wait' 命令?
How to use a 'wait' command inside any loop?
学习Android发展。
在进入 for
循环中的下一个语句之前,代码必须等待几个 seconds/minutes/hours。
for( i=0; i<number; i++) {
// Do Something
// Then Wait for x hours, y minutes, and z seconds. Then proceed to next command.
// Do some more things.
} //End for loop.
我搜索了这个,但找到了很多答案,例如 thread.Sleep
、Sleep
、try{wait(); } Catch{ }
等...
另外,了解了 Handler
。我可以在 for
循环中使用 Handler
吗?
而是,有没有像wait(x hours, x minutes, x seconds)这样的简单命令;像这样的??
请帮忙!!
Thread.sleep(time_in_miliseconds)
看起来是最简单的解决方案。这是一个静态方法,因此您不需要 Thread class 的实例。
Wait 是一个监视器方法,这意味着您可以从同步块或方法中调用该方法。
为你的情况使用睡眠。
我过去做过 Android 编程,但最近没有。虽然我已经做了很多 Java,但我认为我仍然会有所帮助。
关于处理程序,您似乎可以做到这一点。查看文档后,http://developer.android.com/reference/android/os/Handler.html#handleMessage(android.os.Message),本质上您是 post 从一个线程发送消息或可运行对象并在另一个线程中处理消息。您的 for 循环不会停止,因为您在使用处理程序时正在启动一个新线程。
创建处理程序时,如果它是一条消息或 post 您要发送的可运行对象,则需要覆盖 handleMessage(Message msg) 方法,因为这是在经过适当时间后调用的方法.要在特定时间或延迟一段时间后发送您的消息,您可以使用 postAtTime、postDelayed、sendMessageAtTime 和 sendMessageDelayed 方法,以需要的方法为准。
new Handler() {
public void handleMessage(Message msg) {
// Your code here.
}
}.sendMessageDelayed(yourMessage, theAmountOfTimeInMilles);
此外,在处理您的消息后,如果您想要更新任何用户界面(换句话说,更改任何图形,例如更新标签或更改背景),您需要使用 runOnUiThread 方法,否则会抛出异常:
runOnUiThread(new Runnable() {
public void run() {
// Code including UI code here.
}
});
这取决于你在哪里有循环。如果你 运行 主线程中的循环,你不能 "simply insert a delay" 进入它,因为它会阻止执行,并且 Java 没有像 C# 的 async
& await
到 "easily" 解决这个问题。因此,最简单的方法是:首先,将整个循环移至后台线程。 然后,你可以在需要延迟的地方使用Thread.sleep(…)
。但是,如果你需要更新 UI,你不能直接从后台线程执行此操作,你需要使用 Handler,调用 post(Runnable)
方法(传递的 Runnable 将 运行在主线程上),并且在 Runnable 内部你必须检查 UI 是否仍然存在(因为用户可以 "close" 应用程序,所以你的 Activity/Fragment/View/whatever 可以完成或处于 "bad"状态)
在Android中有一个class可以完成你所说的一切,AsyncTask。
private class YourTaskClassName extends AsyncTask<Void, Integer, Long> {
protected Long doInBackground(Void.. values) {
//Here is where you do the loop
for (int i = 0; i < number; i++) {
...
publishProgress(yourProgress); //Value passed to onProgressUpdate
}
return totalSize; //Value for onPostExecute
}
protected void onProgressUpdate(Integer... progress) {
//Here is what you wanna show while your loop is running in background
setProgressPercent(progress[0]);
}
protected void onPostExecute(Long result) {
//Here is what you wanna do when your loop has finished
}
}
你可以这样称呼它new YourTaskClassName().execute();
学习Android发展。
在进入 for
循环中的下一个语句之前,代码必须等待几个 seconds/minutes/hours。
for( i=0; i<number; i++) {
// Do Something
// Then Wait for x hours, y minutes, and z seconds. Then proceed to next command.
// Do some more things.
} //End for loop.
我搜索了这个,但找到了很多答案,例如 thread.Sleep
、Sleep
、try{wait(); } Catch{ }
等...
另外,了解了 Handler
。我可以在 for
循环中使用 Handler
吗?
而是,有没有像wait(x hours, x minutes, x seconds)这样的简单命令;像这样的??
请帮忙!!
Thread.sleep(time_in_miliseconds)
看起来是最简单的解决方案。这是一个静态方法,因此您不需要 Thread class 的实例。
Wait 是一个监视器方法,这意味着您可以从同步块或方法中调用该方法。 为你的情况使用睡眠。
我过去做过 Android 编程,但最近没有。虽然我已经做了很多 Java,但我认为我仍然会有所帮助。
关于处理程序,您似乎可以做到这一点。查看文档后,http://developer.android.com/reference/android/os/Handler.html#handleMessage(android.os.Message),本质上您是 post 从一个线程发送消息或可运行对象并在另一个线程中处理消息。您的 for 循环不会停止,因为您在使用处理程序时正在启动一个新线程。
创建处理程序时,如果它是一条消息或 post 您要发送的可运行对象,则需要覆盖 handleMessage(Message msg) 方法,因为这是在经过适当时间后调用的方法.要在特定时间或延迟一段时间后发送您的消息,您可以使用 postAtTime、postDelayed、sendMessageAtTime 和 sendMessageDelayed 方法,以需要的方法为准。
new Handler() {
public void handleMessage(Message msg) {
// Your code here.
}
}.sendMessageDelayed(yourMessage, theAmountOfTimeInMilles);
此外,在处理您的消息后,如果您想要更新任何用户界面(换句话说,更改任何图形,例如更新标签或更改背景),您需要使用 runOnUiThread 方法,否则会抛出异常:
runOnUiThread(new Runnable() {
public void run() {
// Code including UI code here.
}
});
这取决于你在哪里有循环。如果你 运行 主线程中的循环,你不能 "simply insert a delay" 进入它,因为它会阻止执行,并且 Java 没有像 C# 的 async
& await
到 "easily" 解决这个问题。因此,最简单的方法是:首先,将整个循环移至后台线程。 然后,你可以在需要延迟的地方使用Thread.sleep(…)
。但是,如果你需要更新 UI,你不能直接从后台线程执行此操作,你需要使用 Handler,调用 post(Runnable)
方法(传递的 Runnable 将 运行在主线程上),并且在 Runnable 内部你必须检查 UI 是否仍然存在(因为用户可以 "close" 应用程序,所以你的 Activity/Fragment/View/whatever 可以完成或处于 "bad"状态)
在Android中有一个class可以完成你所说的一切,AsyncTask。
private class YourTaskClassName extends AsyncTask<Void, Integer, Long> {
protected Long doInBackground(Void.. values) {
//Here is where you do the loop
for (int i = 0; i < number; i++) {
...
publishProgress(yourProgress); //Value passed to onProgressUpdate
}
return totalSize; //Value for onPostExecute
}
protected void onProgressUpdate(Integer... progress) {
//Here is what you wanna show while your loop is running in background
setProgressPercent(progress[0]);
}
protected void onPostExecute(Long result) {
//Here is what you wanna do when your loop has finished
}
}
你可以这样称呼它new YourTaskClassName().execute();