FirstActivity
public class FirstActivity extends AppCompatActivity { private static int count = 0; public static final int SET = 1; //设置一个what标记 private Handler myHandle = new Handler() { //定义Handler对象 @Override public void handleMessage(Message msg) { switch (msg.what) { //判断操作类型 case SET: FirstActivity.this.info.setText("dailei---"+count++); } super.handleMessage(msg); } }; private TextView info = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); super.setContentView(R.layout.activity_main); this.info = (TextView)findViewById(R.id.info); Timer timer = new Timer(); //定义调度器 timer.schedule(new MyTask(),0,1000); //立即开始,每隔1秒增长 } /** * 定义定时调度的具体实现类 */ private class MyTask extends TimerTask{ @Override public void run() { Message msg = new Message(); //定义message msg.what = SET; //操作为设置显示文字 FirstActivity.this.myHandle.sendMessage(msg); //发送消息到子线程 } }}
2. activity_main.xml