Android异步更新UI的四种方式
大家都知道由于性能要求,android要求只能在UI线程中更新UI,要想在其他线程中更新UI,大致有4种方式,下面分别使用四种方式来更新一个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<String,String,String>
{
@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");
}
});
}
}
以上就是四种Android异步更新UI的方式,希望对大家的学习有所帮助。