[Android UI开发] Android进行异步更新UI的四种方式

来源:互联网 发布:美团数据分析专员 编辑:程序博客网 时间:2024/06/05 11:41

大家都知道由于性能要求,android要求只能在UI线程中更新UI,要想在其他线程中更新UI,我大致总结了4种方式,欢迎补充纠正:

  1. 使用Handler消息传递机制;

  2. 使用AsyncTask异步任务;

  3. 使用runOnUiThread(action)方法;

  4. 使用Handler的post(Runnabel r)方法;

下面分别使用四种方式来更新一个TextView。

1.使用Handler消息传递机制

packagecom.example.runonuithreadtest;
importandroid.app.Activity;
importandroid.os.Bundle;
importandroid.os.Handler;
importandroid.widget.TextView;
publicclassMainActivity extendsActivity {
    privateTextView tv;
    Handler handler = newHandler()
    {
        publicvoidhandleMessage(android.os.Message msg) {
            if(msg.what==0x123)
            {
                tv.setText("更新后的TextView");
            }
        };
    };
    @Override
    protectedvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv = (TextView) findViewById(R.id.tv);
        newMyThread().start();
    }
    classMyThread extendsThread
    {
        @Override
        publicvoidrun() {
            //延迟两秒更新
            try{
                Thread.sleep(2000);
            catch(InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            handler.sendEmptyMessage(0x123);
        }
    }
}
回到顶部

2. 使用AsyncTask异步任务

  • 注:更新UI的操作只能在onPostExecute(String result)方法中。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
packagecom.example.runonuithreadtest;
importandroid.app.Activity;
importandroid.os.AsyncTask;
importandroid.os.Bundle;
importandroid.widget.TextView;
publicclassMainActivity extendsActivity {
    privateTextView tv;
    @Override
    protectedvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv = (TextView) findViewById(R.id.tv);
        newYibu().execute();
    }
    classYibu extendsAsyncTask<String, String, String>
    {
        @Override
        protectedString doInBackground(String... params) {
            try{
                Thread.sleep(2000);
            catch(InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            returnnull;
        }
        @Override
        protectedvoidonPostExecute(String result) {
            // TODO Auto-generated method stub
            tv.setText("更新后的TextView");
        }
    }
}
回到顶部

3. 使用runOnUiThread(action)方法

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
packagecom.example.runonuithreadtest;
importandroid.app.Activity;
importandroid.os.Bundle;
importandroid.widget.TextView;
publicclassMainActivity extendsActivity {
    privateTextView tv;
    @Override
    protectedvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv = (TextView) findViewById(R.id.tv);
        newMyThread().start();
    }
    classMyThread extendsThread
    {
        @Override
        publicvoidrun() {
            runOnUiThread(newRunnable() {
                @Override
                publicvoidrun() {
                    // TODO Auto-generated method stub
                        try{
                            //延迟两秒更新
                            Thread.sleep(2000);
                        catch(InterruptedException e) {
                            e.printStackTrace();
                        }
                        tv.setText("更新后的TextView");
                }
            });
        }
    }
}
回到顶部

4. 使用Handler的post(Runnabel r)方法

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
packagecom.example.runonuithreadtest;
importandroid.app.Activity;
importandroid.os.Bundle;
importandroid.os.Handler;
importandroid.widget.TextView;
publicclassMainActivity extendsActivity {
    privateTextView tv;
    @Override
    protectedvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv = (TextView) findViewById(R.id.tv);
        Handler handler = newHandler();
        handler.post(newRunnable(){
            @Override
            publicvoidrun() {
                try{
                    //延迟两秒更新
                    Thread.sleep(2000);
                catch(InterruptedException e) {
                    e.printStackTrace();
                }
                tv.setText("更新后的TextView");
            }
        });
    }
}
原文 http://segmentfault.com/a/1190000003702775

0 0
原创粉丝点击