欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2014年01月11日 15:23:08   作者:  
在Android平臺(tái)中多線程應(yīng)用很廣泛,在UI更新、游戲開發(fā)和耗時(shí)處理(網(wǎng)絡(luò)通信等)等方面都需要多線程,下面是一個(gè)在線程中更新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í)行后看不到效果,并且報(bào)了一個(gè)錯(cuò):android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
在Android中更新UI處理必須由創(chuàng)建它的線程更新,而不能在其他線程中更新。上面的錯(cuò)誤原因就在于此。

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

要解決這個(gè)問題,就要明確主線程和子線程的職責(zé)。主線程的職責(zé)是創(chuàng)建、顯示和更新UI控件、處理UI事件、啟動(dòng)子線程、停止子線程等;子線程的職責(zé)是計(jì)算時(shí)間和向主線程發(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)行后不會(huì)報(bào)之前的錯(cuò),TextView也能正常更新內(nèi)容了。

相關(guān)文章

最新評(píng)論