最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2014年01月11日 15:23:08   作者:  
在Android平臺中多線程應(yīng)用很廣泛,在UI更新、游戲開發(fā)和耗時處理(網(wǎng)絡(luò)通信等)等方面都需要多線程,下面是一個在線程中更新UI的代碼

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()方法中實(shí)現(xiàn)。這樣的處理違背了Android多線程編程規(guī)則,系統(tǒng)會拋出異常。

要解決這個問題,就要明確主線程和子線程的職責(zé)。主線程的職責(zé)是創(chuàng)建、顯示和更新UI控件、處理UI事件、啟動子線程、停止子線程等;子線程的職責(zé)是計算時間和向主線程發(fā)出更新UI消息,而不是直接更新UI。子線程向主線程發(fā)送消息可以用Handler實(shí)現(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();
}
}


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

相關(guān)文章

最新評論

淳安县| 诏安县| 青田县| 台州市| 高安市| 濮阳县| 永川市| 岫岩| 宁陵县| 黔南| 娄烦县| 佛坪县| 华安县| 湄潭县| 通辽市| 井陉县| 抚宁县| 昌乐县| 乌兰县| 泸定县| 铜川市| 东平县| 合江县| 鄯善县| 曲阳县| 门头沟区| 崇阳县| 凤翔县| 伊川县| 三亚市| 黄冈市| 信阳市| 阜南县| 湘乡市| 天全县| 平利县| 三原县| 恩施市| 乐亭县| 萨嘎县| 江津市|