详解android进行异步更新UI的四种方式
大家都知道由于性能要求,Android要求只能在UI线程中更新UI,要想在其他线程中更新UI,我大致总结了4种方式,欢迎补充纠正:
- 使用Handler消息传递机制;
- 使用AsyncTask异步任务;
- 使用runOnUiThread(action)方法;
- 使用Handler的post(Runnabelr)方法;
下面分别使用四种方式来更新一个TextView。
1.使用Handler消息传递机制
packagecom.example.runonuithreadtest;
importandroid.app.Activity;
importandroid.os.Bundle;
importandroid.os.Handler;
importandroid.widget.TextView;
publicclassMainActivityextendsActivity{
privateTextViewtv;
Handlerhandler=newHandler()
{
publicvoidhandleMessage(android.os.Messagemsg){
if(msg.what==0x123)
{
tv.setText("更新后的TextView");
}
};
};
@Override
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv=(TextView)findViewById(R.id.tv);
newMyThread().start();
}
classMyThreadextendsThread
{
@Override
publicvoidrun(){
//延迟两秒更新
try{
Thread.sleep(2000);
}catch(InterruptedExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
handler.sendEmptyMessage(0x123);
}
}
}
2.使用AsyncTask异步任务
注:更新UI的操作只能在onPostExecute(Stringresult)方法中。
packagecom.example.runonuithreadtest;
importandroid.app.Activity;
importandroid.os.AsyncTask;
importandroid.os.Bundle;
importandroid.widget.TextView;
publicclassMainActivityextendsActivity{
privateTextViewtv;
@Override
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv=(TextView)findViewById(R.id.tv);
newYibu().execute();
}
classYibuextendsAsyncTask
{
@Override
protectedStringdoInBackground(String...params){
try{
Thread.sleep(2000);
}catch(InterruptedExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
returnnull;
}
@Override
protectedvoidonPostExecute(Stringresult){
//TODOAuto-generatedmethodstub
tv.setText("更新后的TextView");
}
}
}
3.使用runOnUiThread(action)方法
packagecom.example.runonuithreadtest;
importandroid.app.Activity;
importandroid.os.Bundle;
importandroid.widget.TextView;
publicclassMainActivityextendsActivity{
privateTextViewtv;
@Override
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv=(TextView)findViewById(R.id.tv);
newMyThread().start();
}
classMyThreadextendsThread
{
@Override
publicvoidrun(){
runOnUiThread(newRunnable(){
@Override
publicvoidrun(){
//TODOAuto-generatedmethodstub
try{
//延迟两秒更新
Thread.sleep(2000);
}catch(InterruptedExceptione){
e.printStackTrace();
}
tv.setText("更新后的TextView");
}
});
}
}
}
4.使用Handler的post(Runnabelr)方法
packagecom.example.runonuithreadtest;
importandroid.app.Activity;
importandroid.os.Bundle;
importandroid.os.Handler;
importandroid.widget.TextView;
publicclassMainActivityextendsActivity{
privateTextViewtv;
@Override
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv=(TextView)findViewById(R.id.tv);
Handlerhandler=newHandler();
handler.post(newRunnable(){
@Override
publicvoidrun(){
try{
//延迟两秒更新
Thread.sleep(2000);
}catch(InterruptedExceptione){
e.printStackTrace();
}
tv.setText("更新后的TextView");
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
热门推荐
8 高逼格简短祝福语
10 最简短美好的祝福语
11 元旦搞笑祝福语大全 简短
12 1997年新年祝福语简短
13 外国老人的祝福语简短
14 新年送祝福语大全简短
15 驾考祝福语简短霸气
16 92岁老人祝福语简短
17 转账生日祝福语简短独特
18 孩子新年祝福语简短创意