0
任何人都可以幫助我的項目在下面。我需要使用開始和停止按鈕來停止我的Runnable線程。我會在停止按鈕中放入什麼代碼?如何啓動和停止Runnable
公共類MainActivity擴展ActionBarActivity實現OnClickListener {
Context context;
Runnable runnable;
Handler handler;
Thread myThread;
Button btnstart, btnstop;
boolean Running1 = true;
Toast toaststat;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this;
btnstart = (Button) findViewById(R.id.button1);
btnstop = (Button) findViewById(R.id.button2);
btnstart.setOnClickListener(this);
btnstop.setOnClickListener(this);
}
public class RunTimer implements Runnable {
@Override
public void run() {
while(Running1){
try{
Thread.sleep(3000);}
catch(InterruptedException e){
e.printStackTrace();
}
handler.post(new Runnable(){
@Override
public void run(){
}
});
}
}
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.button1: //START
myThread= new Thread(new RunTimer());
myThread.start();
break;
case R.id.button2: //STOP
break;
}
}