WEB开发网
开发学院手机开发Android 开发 android平台中的Handler和HandlerThread使用方法 阅读

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>

1 2  下一页

Tags:android 平台 Handler

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接