public class MainActivity extends Activity {
Button button;
Button endButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button=(Button) findViewById(R.id.button1);
button.setOnClickListener(new startButton());
endButton=(Button) findViewById(R.id.button2);
//试着监听器
endButton.setOnClickListener(new endButton());
}
//定义接口类
class startButton implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
调用handler的post方法,启动线程
handler.post(updateThread);
}
}
class endButton implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
handler.post(updateThread);
}
}
//创建一个handler的对象
Handler handler=new Handler();
//创建一个线程;
Runnable updateThread=new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
System.out.println("UpdateThread");
//handler的postDelayed方法,设置每隔3秒读入一个handler消息
handler.postDelayed(updateThread,3000);
}
};
}