본문 바로가기

IT 연구회

안드로이드 스레드2 핸들러(handler)

반응형

package com.example.jake;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class Main extends Activity {

Button b1, b2;
TextView text;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
text = (TextView) findViewById(R.id.text);
b1 = (Button) findViewById(R.id.b1);
b2 = (Button) findViewById(R.id.b2);

b1.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v1) {
// TODO Auto-generated method stub

doit();
}
});

b2.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v2) {
// TODO Auto-generated method stub

Toast.makeText(Main.this, "테스트 버튼이 클릭되었습니다.",
Toast.LENGTH_SHORT).show();

}
});

}

private void doit() {

new Thread() {
public void run() {
loop();

}
}.start();
}

private void loop() {

for (int i = 1; i <= 100; i++) {
try {
handler.sendEmptyMessage(i);
Thread.sleep(100);

} catch (InterruptedException e) {

}
}
}

Handler handler = new Handler() {
public void handleMessage(Message msg) {

text.setText("" + msg.what);
};

};

}

반응형

'IT 연구회' 카테고리의 다른 글

이중 스크롤뷰(ScrollView)의 scroll 컨트롤하기  (0) 2013.09.17
용량이 1MB가 넘는 파일을 asset에 담을 때  (0) 2013.09.17
인텐트 (Intent)  (0) 2013.09.17
webview  (0) 2013.09.17
안드로이드 스레드 (handler) 핸들러  (0) 2013.09.17
listview  (0) 2013.09.17
listview 2  (0) 2013.09.17
listview3  (0) 2013.09.17
listview layout xml  (0) 2013.09.17
WifiConfiguration  (0) 2013.08.24