Android Handler and Message and Timer

來源:互聯網
上載者:User

定義相關變數

private Handler uHandler;private TimerTask task;private Timer timer = new Timer();;

實現相關代碼

Handler 部分:用於處理接收到的訊息,根據msg.what部分進行相關訊息處理,其中訊息有來之Timer與線程

        uHandler = new Handler(){        @Override        public void handleMessage(Message msg)        {        super.handleMessage(msg);        switch (msg.what)        {        case 0:        // show Begin time when press Play menu        bTime.setText("Begin time: " + nowdate.format(new Date()));                break;        case 1:        // show current time while runin is on going        cTime.setText("Current time: " + nowdate.format(new Date()));        if (runTime >= 3600)        {        hour ++;        runTime = 0;        }        pTime.setText("Run time: " + hour +":" + runTime/60 + "  ");        break;        case 2:        // no video found message        Toast.makeText(getApplicationContext(), "No Video found!", Toast.LENGTH_LONG).show();        break;        case 3:        // video found message        Toast.makeText(getApplicationContext(), "Runin video was found, get ready!", Toast.LENGTH_LONG).show();                break;        case 4:        // Fail conditions        Log.e(TAG, "Got Message for Fail");        setRuninResults(1);          break;        case 5:        // Fass conditions        setRuninResults(0);        break;                }        }        };

Timer 與Message 部分:實現向Handler發送訊息,該部分主要為後台啟動並執行部分,與線程類似。

                timer.schedule(task, 0, 1000);  /* Now start to Counter */                task = new TimerTask(){        @Override        public void run()        {        runTime ++;        if (hour >= RuninTime)        {        Message msg = new Message();                    msg.what = 5;                    uHandler.sendMessage(msg);        }else        {        Message msg = new Message();                    msg.what = 1;                    uHandler.sendMessage(msg);        }        }        };

Thread 線程部分:線程最簡單的建立方法,通過線程向Handler發送訊息

        new Thread(){        @Override        public void run(){        if (!findRuninVideo("/mnt"))        {        Message msg = new Message();        msg.what = 2;        uHandler.sendMessage(msg);        }        else        {        Message msg = new Message();        msg.what = 3;        uHandler.sendMessage(msg);        runinVideo = playList.get(0);        }        Log.d(TAG, "Thread exit success!");        }        }.start();

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.