Android - 线程中的 Looper.prepare() 与 Activity.runOnUIThread()
Android - Looper.prepare() in a Thread vs Activity.runOnUIThread()
我的问题很简单,但我找不到满意的答案。问题是:如果我在 Runnable
开始时调用 Looper.prepare()
,我的线程 运行 是否在 UI 线程上?
我知道 Looper
用于 MessageQueue
并在线程之间交换数据,但它是否使代码 运行 在 UI 线程上?
下面的代码会解释:
@Override
public void onReceive(final Context context, Intent intent) {
if(intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
Runnable runnable = new Runnable() {
@Override
public void run() {
Looper.prepare();
// ... some code ... //
Looper.loop();
}
};
Thread thread = new Thread(runnable);
thread.start();
}
}
不,它不会使您的话题 运行 进入 UI 话题。有关更多信息,请参阅:Communicating with the UI Thread
我的问题很简单,但我找不到满意的答案。问题是:如果我在 Runnable
开始时调用 Looper.prepare()
,我的线程 运行 是否在 UI 线程上?
我知道 Looper
用于 MessageQueue
并在线程之间交换数据,但它是否使代码 运行 在 UI 线程上?
下面的代码会解释:
@Override
public void onReceive(final Context context, Intent intent) {
if(intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
Runnable runnable = new Runnable() {
@Override
public void run() {
Looper.prepare();
// ... some code ... //
Looper.loop();
}
};
Thread thread = new Thread(runnable);
thread.start();
}
}
不,它不会使您的话题 运行 进入 UI 话题。有关更多信息,请参阅:Communicating with the UI Thread