Java 从Android中的辅助线程调用主线程
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4388415/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Call Main thread from secondary thread in Android
提问by sajjoo
How to call Main thread from secondary thread in Android?
如何从Android中的辅助线程调用主线程?
采纳答案by thoredge
The simplest way is to call runOnUiThread(...) from your thread
最简单的方法是从你的线程调用 runOnUiThread(...)
activity.runOnUiThread(new Runnable() {
public void run() {
... do your GUI stuff
}
});
回答by Jesus Oliva
My recommendation to communicate threads in the same process is sending messages between those threads. It is very easy to manage this situation using Handlers:
我建议在同一进程中通信线程是在这些线程之间发送消息。使用处理程序很容易管理这种情况:
http://developer.android.com/reference/android/os/Handler.html
http://developer.android.com/reference/android/os/Handler.html
Example of use, from Android documentation, to handling expensive work out of the ui thread:
使用示例,来自 Android 文档,用于处理 ui 线程之外的昂贵工作:
public class MyActivity extends Activity {
[ . . . ]
// Need handler for callbacks to the UI thread
final Handler mHandler = new Handler();
// Create runnable for posting
final Runnable mUpdateResults = new Runnable() {
public void run() {
updateResultsInUi();
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
[ . . . ]
}
protected void startLongRunningOperation() {
// Fire off a thread to do some work that we shouldn't do directly in the UI thread
Thread t = new Thread() {
public void run() {
mResults = doSomethingExpensive();
mHandler.post(mUpdateResults);
}
};
t.start();
}
private void updateResultsInUi() {
// Back in the UI thread -- update our UI elements based on the data in mResults
[ . . . ]
}
}
回答by keyboardsurfer
You'll need a Handler
that passes the information back to the main thread.
您需要Handler
将信息传递回主线程。
回答by Tad
Also, it's good to remember that if you get your secondary thread through an AsyncTask, you have the option to call onProgressUpdate(), onPostExecute(), etc., to do work on the main thread.
此外,最好记住,如果您通过 AsyncTask 获取辅助线程,则可以选择调用onProgressUpdate()、 onPostExecute()等,以在主线程上工作。
回答by Ravindra babu
Sample code using HandlerThread
使用HandlerThread 的示例代码
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Handler responseHandler = new Handler(Looper.getMainLooper()){
@Override
public void handleMessage(Message msg) {
//txtView.setText((String) msg.obj);
Toast.makeText(MainActivity.this,
"Result from UIHandlerThread:"+(int)msg.obj,
Toast.LENGTH_LONG)
.show();
}
};
HandlerThread handlerThread = new HandlerThread("UIHandlerThread"){
public void run(){
/* Add your business logic to pupulate attributes in Message
in place of sending Integer 5 as in example code */
Integer a = 5;
Message msg = new Message();
msg.obj = a;
responseHandler.sendMessage(msg);
System.out.println(a);
}
};
handlerThread.start();
}
}
Explanation:
解释:
In above example,
HandlerThread
post aMessage
onHandler
of UI Thread, which has been initialized withLooper
of UI Thread.final Handler responseHandler = new Handler(Looper.getMainLooper())
responseHandler.sendMessage(msg);
sendsMessage
fromHandlerThread
to UI ThreadHandler
.handleMessage
processesMessage
received onMessageQueue
and shows a Toast on UI Thread.
在上述示例中,
HandlerThread
发布一个Message
关于Handler
UI线程,这已被初始化的Looper
UI线程的。final Handler responseHandler = new Handler(Looper.getMainLooper())
responseHandler.sendMessage(msg);
发送Message
从HandlerThread
到UI线程Handler
。handleMessage
进程Message
收到MessageQueue
并在 UI 线程上显示 Toast。