UI Thread 이외에서 UI를 갱신하려고 하면 CalledFromWrongThreadException:Only the original thread that created a view hierarchy can touch its views. 라는 문구를 보게 된다.


Handler


UI의 권한이 있는 Thread의 핸들러에 메시지를 보낸다.

Thread + Handler 조합으로 연속적인 갱신이 가능.



public final Handler handler = new Handler{

@Override

public void handleMessage(Message msg) {

//UI 갱신

}

}



외부에서

xxx.handler.sendEmptyMessage();



Activity.runOnUiThread


UI 갱신 권한이 있는 스레드의 인스턴스를 얻을수 있다면



AActivity.this.runOnUiThread(

new Runnable(){

public void run(){

AActivity.this.xxxx.setText("xxxx");

}}

);



Handler.post 사용


Handler handler = new Handler();


...


handler.post(

new Runnable(

public void run(){

//UI 갱신

}

)

);




그 외의 방법도 있다고 한다. (미확인)


View.post(Runnable)
View.postDelayed(Runnable, long)


Posted by tenn
,