Hander , HandlerThread 用法

Hander , HandlerThread 用法


  1. package com.example.handlerdemo2;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.os.Handler;  
  6. import android.os.Message;  
  7. import android.view.Menu;  
  8. import android.view.View;  
  9. import android.view.View.OnClickListener;  
  10. import android.widget.Button;  
  11. import android.widget.ProgressBar;  
  12.   
  13. public class MainActivity extends Activity {  
  14.   
  15.     private Button startButton = null;  
  16.     private ProgressBar progressBar = null;  
  17.       
  18.     @Override  
  19.     protected void onCreate(Bundle savedInstanceState) {  
  20.         super.onCreate(savedInstanceState);  
  21.         setContentView(R.layout.activity_main);  
  22.           
  23.         startButton = (Button)findViewById(R.id.start_button);  
  24.         progressBar = (ProgressBar)findViewById(R.id.progress_bar);  
  25.         startButton.setOnClickListener(new StartButtonListener());  
  26.     }  
  27.   
  28.     @Override  
  29.     public boolean onCreateOptionsMenu(Menu menu) {  
  30.         // Inflate the menu; this adds items to the action bar if it is present.  
  31.         getMenuInflater().inflate(R.menu.main, menu);  
  32.         return true;  
  33.     }  
  34.       
  35.     class StartButtonListener implements OnClickListener  
  36.     {  
  37.   
  38.         @Override  
  39.         public void onClick(View arg0) {  
  40.             // TODO Auto-generated method stub  
  41.             // Causes the Runnable r to be added to the message queue. The runnable will be run on the thread to which this handler is attached.  
  42.             // 这里把线程加入消息队列, 这里离线程是主线程,   
  43.   
  44.             handler.post(updateThread);  
  45.               
  46.         }  
  47.           
  48.     }  
  49.       
  50.     Handler handler = new Handler(){  
  51.   
  52.         @Override  
  53.         public void handleMessage(Message msg) {  
  54.             // TODO Auto-generated method stub  
  55.             super.handleMessage(msg);  
  56.             System.out.println("handleMessage");  
  57.             if(msg.arg1 > 100)  
  58.             {  
  59.                 handler.removeCallbacks(updateThread);  
  60.             }  
  61.             else  
  62.             {  
  63.                 progressBar.setProgress(msg.arg1);  
  64.                 handler.post(updateThread);  
  65.             }  
  66.         }  
  67.           
  68.     };  
  69.     Runnable updateThread = new Runnable(){  
  70.   
  71.         int i = 0;  
  72.         @Override  
  73.         public void run() {  
  74.             // TODO Auto-generated method stub  
  75.             System.out.println("Begin thread");  
  76.             i += 10;  
  77.             Message msg = handler.obtainMessage();  
  78.             msg.arg1 = i;  
  79.             try{  
  80.                 Thread.sleep(1000);  
  81.             }catch(InterruptedException ex){  
  82.                 ex.printStackTrace();  
  83.             }  
  84.             //Pushes a message onto the end of the message queue after all pending messages before the current time.   
  85.             //It will be received in handleMessage(Message), in the thread attached to this handler.  
  86.   
  87.             handler.sendMessage(msg);  
  88.             //视频是在这里做半段, 在这里做判断会导致死循环,因为 removeCallbacks 把线程移出消息队列,并没有删除,   
  89.             //而 当调用sendMessage(msg) 后会调用 handlerMessage()这个方法,又把线程加入消息队列  
  90. //          if(i >= 100)  
  91. //          {  
  92. //              System.out.println("removeCallbacks");  
  93. //              handler.removeCallbacks(updateThread);  
  94. //          }  
  95.         }  
  96.           
  97.     };  
  98.       
  99. }  

下面是 HandlerThread  ,开辟新线程

  1. package com.example.handlerthreaddemo2;  
  2.   
  3. import android.annotation.SuppressLint;  
  4. import android.app.Activity;  
  5. import android.os.Bundle;  
  6. import android.os.Handler;  
  7. import android.os.HandlerThread;  
  8. import android.os.Looper;  
  9. import android.os.Message;  
  10. import android.view.Menu;  
  11.   
  12. public class MainActivity extends Activity {  
  13.   
  14.     @Override  
  15.     protected void onCreate(Bundle savedInstanceState) {  
  16.         super.onCreate(savedInstanceState);  
  17.         setContentView(R.layout.activity_main);  
  18.           
  19.         //生成 HandlerThread对象  
  20.         HandlerThread handlerThread = new HandlerThread("handler_thread");   
  21.         // 在 调用handlerThread.getLooper() 之前一定要  start() ,否则调用getLooper()取得空  
  22.         handlerThread.start();  
  23.         MyHandler handler = new MyHandler(handlerThread.getLooper());  
  24.         Message msg = handler.obtainMessage();  
  25.         //Bundle 用来传递数据, 可以简单看成特别的map , key 只能是String ,   
  26.         Bundle data = new Bundle();  
  27.         data.putInt("age"11);  
  28.         data.putString("name""zhangsan");  
  29.         msg.setData(data);  
  30.         msg.sendToTarget();  
  31.           
  32.     }  
  33.   
  34.     @Override  
  35.     public boolean onCreateOptionsMenu(Menu menu) {  
  36.         // Inflate the menu; this adds items to the action bar if it is present.  
  37.         getMenuInflater().inflate(R.menu.main, menu);  
  38.         return true;  
  39.     }  
  40.   
  41.     class MyHandler extends Handler{  
  42.         public MyHandler()  
  43.         {  
  44.               
  45.         }  
  46.           
  47.         public MyHandler(Looper looper)  
  48.         {  
  49.             super(looper);  
  50.         }  
  51.           
  52.         @Override  
  53.         public void handleMessage(Message msg) {  
  54.             // TODO Auto-generated method stub  
  55.             super.handleMessage(msg);  
  56.             Bundle bundle = msg.getData();  
  57.             System.out.println("name:" + bundle.get("name") + "age:" + bundle.get("age"));  
  58.             System.out.println("myHandler" + Thread.currentThread().getId());  
  59.             //这里打印  名字  是   handler_thread 开始构造函数输入的名称  
  60.             System.out.println("myHandler" + Thread.currentThread().getName());  
  61.         }  
  62.           
  63.           
  64.     }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。
经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值