반응형
package test.Android_Thread;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.TextView;
public class Android_Thread extends Activity {
/** Called when the activity is first created. */
TextView tv,tv2;
String str, str2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//xml에서 지정된 텍스트뷰의 아이디를 가져온다.
tv = (TextView)(findViewById(R.id.text));
tv2 = (TextView)(findViewById(R.id.text2));
//버튼1 처리하는 부분
(findViewById(R.id.button1)).setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Th th = new Th();
th.setDaemon(true); //메인스레드가 완료되면 자동으로 데몬스레드도 완료가 된다.
th.start(); //run함수 호출
}
});
//버튼2 처리하는 부분
(findViewById(R.id.button2)).setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
_threadMethod2();
}
});
}//oncreate
//스레드 상속받은 클래스
public class Th extends Thread{
public void run(){
for(int i=0; i<100; i++){
str = ("Thread1 = " + i);
android.util.Log.e("th", str);
//핸들러 클래스의 handleMessage로 송신한다.
handler.sendEmptyMessage(0);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
private void _threadMethod2(){
// TODO Auto-generated method stub
new Thread(){
public void run(){
for(int i=100; i>0; i--){
str2 = ("Thread2 = " + i);
android.util.Log.e("Thread2", str2);
handler.sendEmptyMessage(1);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(i==1) i=100;
}
}
}.start();
}
Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
if(msg.what == 0){
tv.setText(str);
}
if(msg.what == 1){
tv2.setText(str2);
}
}
};
}//root
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.TextView;
public class Android_Thread extends Activity {
/** Called when the activity is first created. */
TextView tv,tv2;
String str, str2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//xml에서 지정된 텍스트뷰의 아이디를 가져온다.
tv = (TextView)(findViewById(R.id.text));
tv2 = (TextView)(findViewById(R.id.text2));
//버튼1 처리하는 부분
(findViewById(R.id.button1)).setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Th th = new Th();
th.setDaemon(true); //메인스레드가 완료되면 자동으로 데몬스레드도 완료가 된다.
th.start(); //run함수 호출
}
});
//버튼2 처리하는 부분
(findViewById(R.id.button2)).setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
_threadMethod2();
}
});
}//oncreate
//스레드 상속받은 클래스
public class Th extends Thread{
public void run(){
for(int i=0; i<100; i++){
str = ("Thread1 = " + i);
android.util.Log.e("th", str);
//핸들러 클래스의 handleMessage로 송신한다.
handler.sendEmptyMessage(0);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
private void _threadMethod2(){
// TODO Auto-generated method stub
new Thread(){
public void run(){
for(int i=100; i>0; i--){
str2 = ("Thread2 = " + i);
android.util.Log.e("Thread2", str2);
handler.sendEmptyMessage(1);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(i==1) i=100;
}
}
}.start();
}
Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
if(msg.what == 0){
tv.setText(str);
}
if(msg.what == 1){
tv2.setText(str2);
}
}
};
}//root
반응형