1 protected void onCreate(Bundle savedInstanceState) { 2 super.onCreate(savedInstanceState); 3 setContentView(R.layout.activity_main); 4 btn = (Button) findViewById(R.id.button1); 5 progressBar = (ProgressBar) findViewById(R.id.progressBar1); 6 7 btn.setOnClickListener(new OnClickListener() { 8 @Override 9 public void onClick(View v) { 10 // 立即把消息加到消息队列中,postDelayed(r,3000)延迟3秒加到消息队列 11 handler.post(r); 12 } 13 }); 14 } 15 16 Handler handler = new Handler() { 17 public void handleMessage(android.os.Message msg) { 18 progressBar.setProgress(msg.arg1); 19 handler.post(r); 20 21 if (i == 80) 22 // 移除消息 23 handler.removeCallbacks(r); 24 }; 25 26 }; 27 int i = 0; 28 Runnable r = new Runnable() { 29 30 @Override 31 public void run() { 32 i = i + 10; 33 message = new Message(); 34 message.arg1 = i; 35 36 try { 37 Thread.sleep(1000); 38 } catch (InterruptedException e) { 39 // TODO Auto-generated catch block 40 e.printStackTrace(); 41 } 42 43 handler.sendMessage(message); 44 45 } 46 }; 47 }
在第11行不是使用post启动线程r了吗,为什么在第19行还要再post一次??求指点