1. Thread 클래스 사용
public class ThreadTest extends Activity {
int mMainValue = 0;
int mBackValue = 0;
TextView mMainText;
TextView mBackText;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.thread_threadtest);
mMainText = (TextView)findViewById(R.id.mainvalue);
mBackText = (TextView)findViewById(R.id.backvalue);
Button btn = (Button)findViewById(R.id.increase);
btn.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
mMainValue++;
mMainText.setText("MainValue : " + mMainValue);
mBackText.setText("BackValue : " + mBackValue);
}
});
BackThread thread = new BackThread();
thread.setDaemon(true);
thread.start();
}
class BackThread extends Thread {
public void run() {
while (true) {
mBackValue++;
//mBackText.setText("BackValue : " + mBackValue);
try {
Thread.sleep(1000);
} catch (InterruptedException e) { }
}
}
} // end of main
} // end of class
2. Runnable 인터페이스 사용
public class ThreadTest extends Activity {
int mMainValue = 0;
int mBackValue = 0;
TextView mMainText;
TextView mBackText;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.thread_threadtest);
mMainText = (TextView)findViewById(R.id.mainvalue);
mBackText = (TextView)findViewById(R.id.backvalue);
Button btn = (Button)findViewById(R.id.increase);
btn.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
mMainValue++;
mMainText.setText("MainValue : " + mMainValue);
mBackText.setText("BackValue : " + mBackValue);
}
});
BackRunnable runnable = new BackRunnable();
Thread thread = new Thread(runnable);
thread.setDaemon(true);
thread.start();
}
class BackRunnable implements Runnable {
public void run() {
while (true) {
mBackValue++;
try {
Thread.sleep(1000);
} catch (InterruptedException e) { }
}
}
}
}
'Lecture Notes > Android Basics' 카테고리의 다른 글
안드로이드 멀티해상도(Multi-Resolution) (0) | 2012.05.04 |
---|---|
Android View & SurfaceView (0) | 2012.03.22 |
Android Code Name (0) | 2012.02.08 |
안드로이드스타일 안드로이드 기본 프로그램 (0) | 2010.07.20 |
자바스타일 안드로이드 기본 프로그램 (0) | 2010.07.20 |