handler的使用(一)
2014-05-17 21:06
169 查看
handler的常用三个属性是:post(将消息传递到队列) removebacks(回调消息队列) postdelayed(一次将消息压入队列)
handlermessage与message组成实现handler异步消息传递机制
通过message的两个参数arg1 和arg2,实现消息的传递
如下以progressbar的进度条加载为例:
handlermessage与message组成实现handler异步消息传递机制
通过message的两个参数arg1 和arg2,实现消息的传递
如下以progressbar的进度条加载为例:
public class Handleractivity extends Activity { //定义两个控件 private Button button1; private ProgressBar bar1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //获取这两个控件 button1=(Button) findViewById(R.id.button1); // button2=(Button) findViewById(R.id.button2); bar1=(ProgressBar) findViewById(R.id.bar1); //建立监听器 button1.setOnClickListener(new startonclicklistener()); } class startonclicklistener implements OnClickListener{ @Override public void onClick(View v) { // TODO Auto-generated method stub //马上将线程对象加入到消息队列中去 bar1.setVisibility(View.VISIBLE); //将消息压入队列 handler.post(updata); } } //创建一个Handler对象 Handler handler=new Handler(){ @Override public void handleMessage(Message msg) { // TODO Auto-generated method stub //继续接受线程消息传送过来的消息 bar1.setProgress(msg.arg1); //继续将线程消息压入队列 handler.post(updata); } }; //将要执行的操作写在线程对象的run方法中 Runnable updata=new Runnable() { int i=0; @Override public void run() { i=i+10; System.out.println("updata"); msg.arg1=i; try { Thread.sleep(3000); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } //将消息传递给handler队列 handler.sendMessage(msg); //当i=100的时候回调消息,不在压入队列 if(i==100){ handler.removeCallbacks(updata); } } }; }
相关文章推荐
- 使用HttpHandler实现URL重写
- 在HttpHandlers (ASHX files)中使用Session
- 在asp.net中使用HttpHandler为网站图片添加水印
- Struts+Spring+Hibernate项目中使用log4j时发生如下错误"org.apache.log4j.spi.ErrorHandler"
- 在自定义HttpHandler中使用Session
- 使用IConfigurationSectionHandler在web.config中增加自定义配置
- [原创]自定义Config例子,使用IConfigurationSectionHandler 接口
- 使用HttpHandler实现URL重写
- 怎样在自定义HTTPHANDLER中使用SESSION的方法?
- 使用HttpHandler+Cache实现站点访问量的统计
- 使用HttpHandler实现URL重写
- 使用HttpHandler实现URL重写
- 使用handler"不落地"返回图片信息
- 在自定义HttpHandler中使用Session
- 在自定义HttpHandler 中使用Session
- MSDN: 在 ASP.NET 中使用 HTTPHandler 实现 Front Controller
- 小技巧: 当ACE_Svc_Handler关闭时使用的默认行为
- Spring HandlerInterceptor的使用
- 使用HttpHandler实现URL重写
- 使用HttpHandler 实现无页面退出和防止盗链