Android多線程

在新線程中運行函數,函數運行完畢後再執行進一步動作:

private final Handler handler = new Handler() {
@Override
public void handleMessage(Message message) {
// Code Here to do something after the following thread

String str = (String)message.obj; //獲得傳遞過來的對象
}
};


// 在想要開啓線程處:

Runnable runnable = new Runnable() {
public void run() {

// Code Here

//Notify UI thread afterwards using Handler
Message msg = handler.obtainMessage(); //或者:Message msg = handler.obtainMessage(1, obj); //obj 是要傳遞的參數

handler.sendMessage(msg);
}
};
new Thread(runnable).start();

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章