package kr.co.naver;
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 SlotActivity extends Activity
implements OnClickListener, Runnable {
/** Called when the activity is first created. */
private TextView display;
private TextView display1;
private TextView display2;
CounterThread thread = null;
private Handler handler = null;
private boolean isStart = false;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findViewById(R.id.button).setOnClickListener(this);
display = (TextView)findViewById(R.id.txt1);
display1 = (TextView)findViewById(R.id.txt2);
display2 = (TextView)findViewById(R.id.txt3);
handler = new Handler();
}
public void onClick(View v) {
if( isStart == true ) {
isStart = false;
((Button)v).setText( "Start ");
} else {
isStart = true;
((Button)v).setText( "Stop ");
}
if( thread == null) {
thread = new CounterThread(this);
thread.start();
Toast.makeText(this,"start", Toast.LENGTH_SHORT ).show();
}else {
thread = new CounterThread(this);
thread.stop();
Toast.makeText(this,"stop", Toast.LENGTH_SHORT ).show();
}
}
public void run() {
int n1, n2, n3;
n1 = (int)Math.round(Math.random() * 10);
n2 = (int)Math.round(Math.random() * 10);
n3 = (int)Math.round(Math.random() * 10);
display.setText("1st" +
Integer.toString(n1));
display1.setText("2nd" +
Integer.toString(n2));
display2.setText("3rd" +
Integer.toString(n3));
}
public Handler getHandler() {
return handler;
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
package kr.co.naver;
import android.os.Handler;
public class CounterThread extends Thread {
private SlotActivity owner;
public CounterThread( SlotActivity obj){
owner = obj;
}
public void run() {
while( true ) {
owner.getHandler().post( owner );
try {
sleep(1000);
}catch(Exception e) { }
}
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/txt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
<TextView
android:id="@+id/txt2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
<TextView
android:id="@+id/txt3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
<Button
android:id="@+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#FFFFFF"
android:text="start" />
</LinearLayout>
지금은 start로 누르면 난수가 발생해서 돌아가는 것만 나옵니다
여기서 stop을 누르면 멈추게 하려는데 어떻게 해야 되나요?




쓰레드 죽이면 되죠...스톱메소드 만드시고...스레드 죽이는 헨들러 메시지 작성해서 날리시면..헨들러에서 쓰레드 인터럽트 걸어버리면 됩니다..