«

Android 子线程 更新 UI 界面 总结

时间:2024-3-2 17:55     作者:韩俊     分类: Android


package com.jrhcode.morethreadtest;

import java.util.concurrent.Executors;

import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 
 * 主要功能是:在子线程中来更改TextView的显示内容
 * 
 * android 多线程编程
   1:通过handler 在新启动的线程中更新UI组件 Handler.post(new Runnable());
   2:handler.postdelayed(new Runnable, delay);
   3: Activity.runOnUiThread(); 
   4:AsyncTask 
   5:线程池
 * 
 * @author Administrator
 * 
 */
public class MainActivity extends Activity {

    TextView tv;
    private Handler mHandler;
    private String TAG = MainActivity.class.getSimpleName();
    int i = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv = (TextView) findViewById(R.id.tv);
        mHandler = new Handler();
        // initHandler();
        // initHandler2();
        // initHandler3();
        // updateTv();
        // updateTVV();
        threedPool();
    }

    private void threedPool() {
        // TODO Auto-generated method stub
        Executors.newSingleThreadExecutor().submit(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                System.out.println(Thread.currentThread().getName());
                tv.setText(TAG);
                mHandler.post(new Runnable() {

                    @Override
                    public void run() {
                        // TODO Auto-generated method stub
                        tv.setText("this is handler post");
                    }
                });
            }
        });

    }

    /**
 * 
 */
    private void updateTVV() {
        // TODO Auto-generated method stub
        new MyTask().execute();
    }

    /**
     * 方法3
     */
    private void updateTv() {
        // TODO Auto-generated method stub
        new Thread(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                MainActivity.this.runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        // TODO Auto-generated method stub
                        tv.setText(TAG);
                    }
                });
            }
        }).start();
    }

    /**
     * 延迟3秒
     */
    private void initHandler3() {
        // TODO Auto-generated method stub
        new Thread(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub

                mHandler.postDelayed(new Runnable() {

                    @Override
                    public void run() {
                        // TODO Auto-generated method stub
                        tv.setText(TAG);
                    }
                }, 3000);
            }
        }).start();
    }

    /**
     * 在子线程中方法
     */
    private void initHandler2() {
        // TODO Auto-generated method stub
        new Thread(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                mHandler.post(new Runnable() {

                    @Override
                    public void run() {
                        System.out.println(Thread.currentThread().getName());
                        tv.setText(TAG);
                    }
                });
            }
        }).start();

    }

    /**
     * Mothod 1; 该方法是在注线程中操作UI;
     */
    private void initHandler() {
        // TODO Auto-generated method stub

        mHandler.post(new Runnable() {

            @Override
            public void run() {
                Toast.makeText(MainActivity.this,
                        Thread.currentThread().getName(), 0).show();
                tv.setText(TAG);
            }
        });
    }

    class MyTask extends AsyncTask<String, Void, String> {

        @Override
        protected String doInBackground(String... params) {
            // TODO Auto-generated method stub
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            return null;
        }

        @Override
        protected void onPostExecute(String result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);

            tv.setText(TAG);
        }

    }

}

标签: android

热门推荐