Android handler.post problem

 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                 // Immediately put the message to the message queue, postDelayed (R, 3000) delay of 3 seconds is added to the message queue
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                 // Remove message
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 }

In the eleventh line is not using the post to start the thread r now, why in the nineteenth line to post again?? seeking advice

Posted by Megan at November 19, 2013 - 12:33 PM