Handler机制

    利用Handler来实现UI线程的更新的。

  Handler来根据接收的消息,处理UI更新。Thread线程发出Handler消息,通知更新UI。

Handler myHandler = new Handler() { 
          public void handleMessage(Message msg) {  
               switch (msg.what) {  
                    case TestHandler.GUIUPDATEIDENTIFIER:  
                         myBounceView.invalidate();  //更新UI
                         break;  
               }  
               super.handleMessage(msg);  
          }  
     }; 

class myThread implements Runnable {  
          public void run() { 
while (!Thread.currentThread().isInterrupted()) {   
                      
                    Message message = new Message();  
                    message.what = TestHandler.GUIUPDATEIDENTIFIER;  
                     
                    TestHandler.this.myHandler.sendMessage(message);   //发送消息
                    try {  
                         Thread.sleep(100);   
                    } catch (InterruptedException e) {  
                         Thread.currentThread().interrupt();  
                    }  
               }  
          }  
     }  

posted @ 2016-07-30 11:31  朱小勇  阅读(221)  评论(0编辑  收藏  举报