对Android中handler处理两个线程的理解

我看了网上的教程是这样的,用handler除了一个线程,就是控制progressbar。于是我拓展了一下,我另外写了个计数的线程。先看源代码。

public class HandlerTestActivity extends Activity {       /** Called when the activity is first created. */              private ProgressBar pbar=null;       private Button startBtn = null;       private Button endBtn = null;              private TextView timerView = null;       private Button endtimer = null;              @Override       public void onCreate(Bundle savedInstanceState) {           super.onCreate(savedInstanceState);           setContentView(R.layout.main);                      pbar = (ProgressBar)findViewById(R.id.pbar);           pbar.setMax(100);                      startBtn = (Button)findViewById(R.id.startbtn);           endBtn = (Button)findViewById(R.id.endbtn);           timerView = (TextView)findViewById(R.id.timerView);           endtimer = (Button)findViewById(R.id.endtimer);                      startBtn.setText("开始");           startBtn.setOnClickListener(new OnClickListener(){               public void onClick(View v) {                   pbar.setVisibility(View.VISIBLE);                   handler.post(runnable);                   handler.post(timerRunnabler);               }           });           endBtn.setText("结束");           endBtn.setOnClickListener(new OnClickListener(){               public void onClick(View v) {                   pbar.setProgress(0);                   handler.removeCallbacks(runnable);               }           });                      endtimer.setOnClickListener(new OnClickListener(){               public void onClick(View v) {                   handler.removeCallbacks(timerRunnabler);               }           });       }                     Handler handler = new Handler(){           @Override           public void handleMessage(Message msg){               int intmsg = msg.what;               switch(intmsg){                   case 0:                       pbar.setProgress(msg.arg1);                       if(msg.arg1<pbar.getMax()){                           handler.post(runnable);                            }else{                           handler.removeCallbacks(runnable);                       }                       break;                   case 1:                       timerView.setText("倒计时"+msg.arg1);                       handler.post(timerRunnabler);                       break;               }           }       };        Runnable runnable = new Runnable(){           int i = 0;           public void run(){               i= i+ 5;               Message msg=new Message();               msg.arg1=i;               msg.what=0;               Log.d("System.out","i="+i+"----runnableId="+Thread.currentThread().getId()+"|----runnableName="+Thread.currentThread().getName());               try{                   Thread.sleep(3000);                }catch(InterruptedException e){                   e.printStackTrace();               }               handler.sendMessage(msg);                              if(i==80){                   pbar.setProgress(0);                   handler.removeCallbacks(runnable);                   //handler.removeMessages(0);                }           }       };              //计数器        Runnable timerRunnabler = new Runnable(){              int j=0;           public void run() {               j=j+1;               Message msg = new Message();               msg.arg1=j;               msg.what=1;               Log.d("System.out","j="+j+"----timerRunnablerId="+Thread.currentThread().getId()+"|----timerRunnablerName="+Thread.currentThread().getName());                  try{                   Thread.sleep(1000);               }catch(InterruptedException e){                   e.printStackTrace();               }               handler.sendMessage(msg);           }       };   }  

操作界面是这样的

对Android中handler处理两个线程的理解

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wwjjwf.html