国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

android使用多線程更新ui示例分享

2020-04-11 11:57:26
字體:
供稿:網(wǎng)友

Android線程涉及的技術(shù)有:Handler;Message;MessageQueue;Looper;HandlerThread。

下面看一段在線程中更新UI的代碼:

復(fù)制代碼 代碼如下:

public class MainActivity extends Activity {
private TextView timeLable;
private Button stopBtn;
private Thread mThread;
private boolean isRunning = true;
private int timeCount = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    timeLable = (TextView) findViewById(R.id.timelable);
    stopBtn = (Button) findViewById(R.id.stop);
    stopBtn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {              
            isRunning = false;
        }
    });

    mThread = new Thread(new Runnable() {

        @Override
        public void run() {
            while (isRunning) {
                try {
                    Thread.sleep(1000);
                    timeCount++;
                    timeLable.setText("timeCount=" + timeCount + " 秒");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

        }
    });
    mThread.start();       
}
}

這段代碼只是在線程中更新TextView的顯示內(nèi)容,但是執(zhí)行后看不到效果,并且報了一個錯:android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
在Android中更新UI處理必須由創(chuàng)建它的線程更新,而不能在其他線程中更新。上面的錯誤原因就在于此。

由于timeLable是一個UI控件,它是在主線程中創(chuàng)建的,但是它卻在子線程中被更新了,更新操作在mThread線程的run()方法中實現(xiàn)。這樣的處理違背了Android多線程編程規(guī)則,系統(tǒng)會拋出異常。

要解決這個問題,就要明確主線程和子線程的職責(zé)。主線程的職責(zé)是創(chuàng)建、顯示和更新UI控件、處理UI事件、啟動子線程、停止子線程等;子線程的職責(zé)是計算時間和向主線程發(fā)出更新UI消息,而不是直接更新UI。子線程向主線程發(fā)送消息可以用Handler實現(xiàn)。代碼如下:

復(fù)制代碼 代碼如下:

public class MainActivity extends Activity {

private TextView timeLable;
private Button stopBtn;
private Thread mThread;
private boolean isRunning = true;
private int timeCount = 0;

final private Handler mHandler = new Handler(){
    public void handleMessage(Message msg) {
        switch (msg.what) {
        case 0 :
            timeLable.setText("timeCount=" + timeCount + " 秒");
            break;
        default :
            break;
        }
    }
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    timeLable = (TextView) findViewById(R.id.timelable);
    stopBtn = (Button) findViewById(R.id.stop);
    stopBtn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            isRunning = false;
        }
    });

    mThread = new Thread(new Runnable() {

        @Override
        public void run() {
            while (isRunning) {
                try {
                    Thread.sleep(1000);
                    timeCount++;
                    mHandler.sendEmptyMessage(0);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

        }
    });
    mThread.start();
}
}


運行后不會報之前的錯,TextView也能正常更新內(nèi)容了。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 五家渠市| 乐业县| 同德县| 抚宁县| 聊城市| 金昌市| 安龙县| 沂南县| 连州市| 南华县| 长治市| 修武县| 海兴县| 江源县| 鄂尔多斯市| 原阳县| 芦溪县| 天长市| 东海县| 乌兰浩特市| 陵水| 新蔡县| 建瓯市| 容城县| 惠州市| 牙克石市| 区。| 威海市| 芦山县| 新宁县| 石嘴山市| 易门县| 德钦县| 寻甸| 米林县| 来安县| 和政县| 木里| 武清区| 齐齐哈尔市| 甘德县|