zoukankan      html  css  js  c++  java
  • Android异步更新

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

    使用Handler消息传递机制;

    使用AsyncTask异步任务;

    使用runOnUiThread(action)方法;

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

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

    1.使用Handler消息传递机制

    public class MainActivity extends Activity {
        private TextView tv;
        Handler handler = new Handler()
        {
            public void handleMessage(android.os.Message msg) {
              
                    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();
            }
        }
    }

    2.使用AsyncTask异步任务

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

    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)方法

    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)方法

    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");
                }
            });
        }
    }
  • 相关阅读:
    CF1051F The Shortest Statement 题解
    CF819B Mister B and PR Shifts 题解
    HDU3686 Traffic Real Time Query System 题解
    HDU 5969 最大的位或 题解
    P3295 萌萌哒 题解
    BZOJ1854 连续攻击游戏 题解
    使用Python编写的对拍程序
    CF796C Bank Hacking 题解
    BZOJ2200 道路与航线 题解
    USACO07NOV Cow Relays G 题解
  • 原文地址:https://www.cnblogs.com/wei1228565493/p/4896488.html
Copyright © 2011-2022 走看看