android平台中的Handler和HandlerThread使用方法
2012-12-25 19:10:58 来源:WEB开发网核心提示: Handler使用方法: Handler handler <span style="color:#339933">=</span> <span style="color:#000000; font-weight:bold">ne
Handler使用方法:
Handler handler <span style="color:#339933">=</span> <span style="color:#000000; font-weight:bold">new</span> Handler<span style="color:#0990">(</span><span style="color:#0990">)</span> <span style="color:#0990">{</span> @Override <span style="color:#000000; font-weight:bold">public</span> <span style="color:#0066; font-weight:bold">void</span> handleMessage<span style="color:#0990">(</span>Message msg<span style="color:#0990">)</span> <span style="color:#0990">{</span> <span style="color:#666666; font-style:italic">// 处理发送过来的消息</span> Bundle b <span style="color:#339933">=</span> msg.<span style="color:#06633">getData</span><span style="color:#0990">(</span><span style="color:#0990">)</span><span style="color:#339933">;</span> <span style="color:#03399">System</span>.<span style="color:#06633">out</span>.<span style="color:#06633">println</span><span style="color:#0990">(</span><span style="color:#00ff">"msg:"</span> <span style="color:#339933">+</span> msg.<span style="color:#06633">arg1</span><span style="color:#0990">)</span><span style="color:#339933">;</span> <span style="color:#03399">System</span>.<span style="color:#06633">out</span>.<span style="color:#06633">println</span><span style="color:#0990">(</span><span style="color:#00ff">"msg:"</span> <span style="color:#339933">+</span> b.<span style="color:#06633">getString</span><span style="color:#0990">(</span><span style="color:#00ff">"name"</span><span style="color:#0990">)</span> <span style="color:#339933">+</span> <span style="color:#00ff">" - age:"</span> <span style="color:#339933">+</span> b.<span style="color:#06633">getInt</span><span style="color:#0990">(</span><span style="color:#00ff">"age"</span><span style="color:#0990">)</span><span style="color:#0990">)</span><span style="color:#339933">;</span> <span style="color:#000000; font-weight:bold">super</span>.<span style="color:#06633">handleMessage</span><span style="color:#0990">(</span>msg<span style="color:#0990">)</span><span style="color:#339933">;</span> <span style="color:#0990">}</span> <span style="color:#0990">}</span><span style="color:#339933">;</span> Message msg <span style="color:#339933">=</span> handler.<span style="color:#06633">obtainMessage</span><span style="color:#0990">(</span><span style="color:#0990">)</span><span style="color:#339933">;</span> msg.<span style="color:#06633">arg1</span> <span style="color:#339933">=</span> <span style="color:#cc66cc">121</span><span style="color:#339933">;</span> Bundle b <span style="color:#339933">=</span> <span style="color:#000000; font-weight:bold">new</span> Bundle<span style="color:#0990">(</span><span style="color:#0990">)</span><span style="color:#339933">;</span> b.<span style="color:#06633">putInt</span><span style="color:#0990">(</span><span style="color:#00ff">"age"</span>, <span style="color:#cc66cc">24</span><span style="color:#0990">)</span><span style="color:#339933">;</span> b.<span style="color:#06633">putString</span><span style="color:#0990">(</span><span style="color:#00ff">"name"</span>, <span style="color:#00ff">"Fatkun"</span><span style="color:#0990">)</span><span style="color:#339933">;</span> msg.<span style="color:#06633">setData</span><span style="color:#0990">(</span>b<span style="color:#0990">)</span><span style="color:#339933">;</span> msg.<span style="color:#06633">sendToTarget</span><span style="color:#0990">(</span><span style="color:#0990">)</span><span style="color:#339933">;</span> handler.<span style="color:#06633">post</span><span style="color:#0990">(</span>r<span style="color:#0990">)</span><span style="color:#339933">;</span> <span style="color:#0990">}</span> <span style="color:#03399">Runnable</span> r <span style="color:#339933">=</span> <span style="color:#000000; font-weight:bold">new</span> <span style="color:#03399">Runnable</span><span style="color:#0990">(</span><span style="color:#0990">)</span> <span style="color:#0990">{</span> <span style="color:#000000; font-weight:bold">public</span> <span style="color:#0066; font-weight:bold">void</span> run<span style="color:#0990">(</span><span style="color:#0990">)</span> <span style="color:#0990">{</span> <span style="color:#000000; font-weight:bold">try</span> <span style="color:#0990">{</span> <span style="color:#666666; font-style:italic">// 在这里只是睡一下</span> <span style="color:#03399">Thread</span>.<span style="color:#06633">sleep</span><span style="color:#0990">(</span><span style="color:#cc66cc">10000</span><span style="color:#0990">)</span><span style="color:#339933">;</span> <span style="color:#0990">}</span> <span style="color:#000000; font-weight:bold">catch</span> <span style="color:#0990">(</span><span style="color:#03399">InterruptedException</span> e<span style="color:#0990">)</span> <span style="color:#0990">{</span> e.<span style="color:#06633">printStackTrace</span><span style="color:#0990">(</span><span style="color:#0990">)</span><span style="color:#339933">;</span> <span style="color:#0990">}</span> <span style="color:#0990">}</span> <span style="color:#0990">}</span><span style="color:#339933">;</span>
- ››Android 当修改一些代码时,使用什么编译命令可以最...
- ››Android 如何添加一个apk使模拟器和真机都编译进去...
- ››Android 修改Camera拍照的默认保存路径
- ››Android 如何修改默认输入法
- ››android开发中finish()和System.exit(0)的区别
- ››Android手势识别简单封装类
- ››android中查看项目数字证书的两种方法
- ››Android中获取IMEI码的办法
- ››android 相机报错 setParameters failed
- ››Android重启运用程序的代码
- ››Android为ListView的Item设置不同的布局
- ››android bitmap与base64字符串的互相转换
更多精彩
赞助商链接