详解android进行异步更新UI的四种方式
程序员文章站
2023-12-12 16:47:58
大家都知道由于性能要求,android要求只能在ui线程中更新ui,要想在其他线程中更新ui,我大致总结了4种方式,欢迎补充纠正:
使用handler消息传递机制...
大家都知道由于性能要求,android要求只能在ui线程中更新ui,要想在其他线程中更新ui,我大致总结了4种方式,欢迎补充纠正:
- 使用handler消息传递机制;
- 使用asynctask异步任务;
- 使用runonuithread(action)方法;
- 使用handler的post(runnabel r)方法;
下面分别使用四种方式来更新一个textview。
1.使用handler消息传递机制
package com.example.runonuithreadtest; import android.app.activity; import android.os.bundle; import android.os.handler; import android.widget.textview; public class mainactivity extends activity { private textview tv; handler handler = new handler() { public void handlemessage(android.os.message msg) { if(msg.what==0x123) { tv.settext("更新后的textview"); } }; }; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); tv = (textview) findviewbyid(r.id.tv); new mythread().start(); } class mythread extends thread { @override public void run() { //延迟两秒更新 try { thread.sleep(2000); } catch (interruptedexception e) { // todo auto-generated catch block e.printstacktrace(); } handler.sendemptymessage(0x123); } } }
2. 使用asynctask异步任务
注:更新ui的操作只能在onpostexecute(string result)方法中。
package com.example.runonuithreadtest; import android.app.activity; import android.os.asynctask; import android.os.bundle; import android.widget.textview; public class mainactivity extends activity { private textview tv; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); tv = (textview) findviewbyid(r.id.tv); new yibu().execute(); } class yibu extends asynctask<string, string, string> { @override protected string doinbackground(string... params) { try { thread.sleep(2000); } catch (interruptedexception e) { // todo auto-generated catch block e.printstacktrace(); } return null; } @override protected void onpostexecute(string result) { // todo auto-generated method stub tv.settext("更新后的textview"); } } }
3. 使用runonuithread(action)方法
package com.example.runonuithreadtest; import android.app.activity; import android.os.bundle; import android.widget.textview; public class mainactivity extends activity { private textview tv; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); tv = (textview) findviewbyid(r.id.tv); new mythread().start(); } class mythread extends thread { @override public void run() { runonuithread(new runnable() { @override public void run() { // todo auto-generated method stub try { //延迟两秒更新 thread.sleep(2000); } catch (interruptedexception e) { e.printstacktrace(); } tv.settext("更新后的textview"); } }); } } }
4. 使用handler的post(runnabel r)方法
package com.example.runonuithreadtest; import android.app.activity; import android.os.bundle; import android.os.handler; import android.widget.textview; public class mainactivity extends activity { private textview tv; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); tv = (textview) findviewbyid(r.id.tv); handler handler = new handler(); handler.post(new runnable(){ @override public void run() { try { //延迟两秒更新 thread.sleep(2000); } catch (interruptedexception e) { e.printstacktrace(); } tv.settext("更新后的textview"); }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。