간단하게 run 부분을 while로 바꾸시고 myThread.stopThread 호출 시 while 문을 종료 하도록 하시면 됩니다.
class MyThread extends Thread
Context rContext;
int icnt = 0;
int boolean stopped = false;
public MyThread(Context context) {
rContext = context;
stopped = false;
}
@Override
public void run() {
// TODO Auto-generated method stub
while(!stopped)
{
blutoothReciveData();
try{ Thread.sleep(반복전 기다릴 시간) } catch(Exception e){}
}
}
public void stopThread(){
stopped = true;
}
void blutoothReciveData(){
handler.post(new Runnable() {
@Override
public void run() {
cnt++;
Toast.makeText(rContext, "cnt = "+cnt,1).show();
}
});
}
}
아니면 아래와 같이 핸들러를 사용해서 매번 수행 후 핸들러가 동작하게 해도 됩니다.
Handler handler = new Handler() {
public void handlerMessage(Message msg) {
switch(msg.what) {
blutoothReciveData();
handler.sendemptymessagedelayed(msg.what, 반복 전 기다릴 시간);
}
}
}