为什么我们使用处理程序?为什么我们不在 Runnable 对象中调用接口元素?
Why do we use a Handler? Why we don't call an interface element inside a Runnable object?
只要执行此代码,应用程序就会崩溃,但是当使用处理程序时,应用程序会按预期工作。
Runnable r = new Runnable() {
@Override
public void run() {
long futuretime = System.currentTimeMillis()+10000;
while(System.currentTimeMillis()<futuretime){
synchronized (this){
try {
wait(futuretime - System.currentTimeMillis());
} catch (Exception e) {}
}
}
//this code needs to be inside a Handler ??
TextView time = (TextView)findViewById(R.id.timedisplay);
time.setText("Changed Man!!");
//this code needs to be inside a Handler ??
}
};
Thread thread = new Thread(r);
thread.start();
}
处理 UI 的代码应该是 UI(主)线程上的 运行。
您(可能)在 UI 线程上创建了一个处理程序,因此通过它发送的所有消息也会 运行 在该线程上。
Runnable 用于后台进程(后台线程),而 textview 在您的 UI 线程中,因此后台线程无法与前台线程通信,因此它会给您带来错误并使您的 application.you 也可以使用runOnUiThread。例如。
Runnable r = new Runnable() {
@Override
public void run() {
long futuretime = System.currentTimeMillis()+10000;
while(System.currentTimeMillis()<futuretime){
synchronized (this){
try {
wait(futuretime - System.currentTimeMillis());
} catch (Exception e) {}
}
}
try {
// code runs in a thread
runOnUiThread(new Runnable() {
@Override
public void run() {
TextView time = (TextView)findViewById(R.id.timedisplay);
time.setText("Changed Man!!");
}
});
} catch (final Exception ex) {
Log.i("---","Exception in thread");
}
}
};
Thread thread = new Thread(r);
thread.start();
您的应用程序崩溃的原因是您从非UI 线程修改视图。
如果您使用属于 UI-thread 的处理程序 执行此操作,这将按预期工作。Update
如果你需要 运行 Runnable 修改 UI 你可以选择:
1) yourActivity.runOnUiThread(可运行 r)
2) yourHandlerOnUIThread.post(Runnable r)
3) yourView.post(Runnable r)
这里所有的答案都提到了处理程序的使用在 Android 和 UI 线程中使用。但让我补充一下。
如果你去过 Android documentation/tutorial 你会知道
When an application component starts and the application does not have
any other components running, the Android system starts a new Linux
process for the application with a single thread of execution. By
default, all components of the same application run in the same
process and thread (called the "main" thread or uiThread).
了解更多信息 refer
现在来看你提到的例子; 您已经使用 Runnable 创建了另一个线程...所以在 Android Application.
中,您可能需要除 mainThread 之外的线程
如果您精通JAVA线程概念,那么您就会知道线程间通信是如何发生的,以及如何以不同的方式完成refer
所以回到问题 在 android 中我们有 mainThread 或 uiThread 所谓的 它持有我们的 ui 即视图组件。 这些组件是 mainThread 私有的,因此其他线程无法访问它...这在之前的回答中已经提到。
这就是 Handler 出现的地方您 不需要担心您的消息如何从一个线程传递到另一个线程。
处理程序
There are two main uses for a Handler: (1) to schedule messages and
runnables to be executed as some point in the future; and (2) to
enqueue an action to be performed on a different thread than your
own.
When posting or sending to a Handler, you can either allow the
item to be processed as soon as the message queue is ready to do so,
or specify a delay before it gets processed or absolute time for it to
be processed. The latter two allow you to implement timeouts, ticks,
and other timing-based behavior.
有关更多信息,请参阅 docs 和
有关 handler and UI thread
的更多信息
只要执行此代码,应用程序就会崩溃,但是当使用处理程序时,应用程序会按预期工作。
Runnable r = new Runnable() {
@Override
public void run() {
long futuretime = System.currentTimeMillis()+10000;
while(System.currentTimeMillis()<futuretime){
synchronized (this){
try {
wait(futuretime - System.currentTimeMillis());
} catch (Exception e) {}
}
}
//this code needs to be inside a Handler ??
TextView time = (TextView)findViewById(R.id.timedisplay);
time.setText("Changed Man!!");
//this code needs to be inside a Handler ??
}
};
Thread thread = new Thread(r);
thread.start();
}
处理 UI 的代码应该是 UI(主)线程上的 运行。
您(可能)在 UI 线程上创建了一个处理程序,因此通过它发送的所有消息也会 运行 在该线程上。
Runnable 用于后台进程(后台线程),而 textview 在您的 UI 线程中,因此后台线程无法与前台线程通信,因此它会给您带来错误并使您的 application.you 也可以使用runOnUiThread。例如。
Runnable r = new Runnable() {
@Override
public void run() {
long futuretime = System.currentTimeMillis()+10000;
while(System.currentTimeMillis()<futuretime){
synchronized (this){
try {
wait(futuretime - System.currentTimeMillis());
} catch (Exception e) {}
}
}
try {
// code runs in a thread
runOnUiThread(new Runnable() {
@Override
public void run() {
TextView time = (TextView)findViewById(R.id.timedisplay);
time.setText("Changed Man!!");
}
});
} catch (final Exception ex) {
Log.i("---","Exception in thread");
}
}
};
Thread thread = new Thread(r);
thread.start();
您的应用程序崩溃的原因是您从非UI 线程修改视图。 Update
如果您使用属于 UI-thread 的处理程序 执行此操作,这将按预期工作。
如果你需要 运行 Runnable 修改 UI 你可以选择:
1) yourActivity.runOnUiThread(可运行 r)
2) yourHandlerOnUIThread.post(Runnable r)
3) yourView.post(Runnable r)
这里所有的答案都提到了处理程序的使用在 Android 和 UI 线程中使用。但让我补充一下。
如果你去过 Android documentation/tutorial 你会知道
When an application component starts and the application does not have any other components running, the Android system starts a new Linux process for the application with a single thread of execution. By default, all components of the same application run in the same process and thread (called the "main" thread or uiThread).
了解更多信息 refer
现在来看你提到的例子; 您已经使用 Runnable 创建了另一个线程...所以在 Android Application.
中,您可能需要除 mainThread 之外的线程
如果您精通JAVA线程概念,那么您就会知道线程间通信是如何发生的,以及如何以不同的方式完成refer
所以回到问题 在 android 中我们有 mainThread 或 uiThread 所谓的 它持有我们的 ui 即视图组件。 这些组件是 mainThread 私有的,因此其他线程无法访问它...这在之前的回答中已经提到。
这就是 Handler 出现的地方您 不需要担心您的消息如何从一个线程传递到另一个线程。
处理程序
There are two main uses for a Handler: (1) to schedule messages and runnables to be executed as some point in the future; and (2) to enqueue an action to be performed on a different thread than your own.
When posting or sending to a Handler, you can either allow the item to be processed as soon as the message queue is ready to do so, or specify a delay before it gets processed or absolute time for it to be processed. The latter two allow you to implement timeouts, ticks, and other timing-based behavior.
有关更多信息,请参阅 docs 和 有关 handler and UI thread
的更多信息