Handler—————–筆記

來源:互聯網
上載者:User

一、Handler是Android作業系統提供的一個類

     隊列是一種(先進先出)的資料結構,棧(先進後出)的資料結後

二、Runable updateThread = new Runable(){}; //將要執行的操作寫線上程對象的run方法中

      Handler handler = new Handler(); //建立一個線程對象

      handler.post(updateThread);  //調用Handler的post方法,將要執行的線程的線程對象加入到隊列當中

      handler.postDelayed(updateThread,3000);  //在run方法內部執行postDelayed或者是post方法

三、bar.setVisibility(View.VISIBLE); //設定進度條處於可見狀態

     Runnable updateThread = new Runable(){}  //線程類該類使用匿名內部類的方式進行聲明

     Message msg = updateBarHandler.obtainMessage();  //得到一個訊息對象,Message類是由Android作業系統提供

     msg.arg1 = i; //將msg對象的arg1參數的設定為i,用arg1和agr2這倆個成員變數傳遞訊息,優點是系統消耗較少

     Thread.sleep(1000);  //設定當前線程睡眠一秒

     updataBarHandler.sendMessage(msg); //將msg對象加入到訊息佇列當中

四、System.out.println(''activity------>" + Thread.CurrentThread().getId());

     System.out.println(''activityname------>" + Thread.CurrentThread().getname());

HandlerThread handlerThread = new HandlerThread("handler_thread");  //產生了一個HandlerThread對象,實現了使用Looper來處理訊息佇列的功能,這個類由Android應用程式架構提供

handlerThread.start();  //在使用HandlerThread的getLooper()方法之前,必須先調用該類的Start()

Myhandler myHandler = new Myhandler(handlerThread.getlooper());

Message msg = myHandler.obtainMessage();  //將msg發送到目標對象,所謂目標對象就是產生msg對象的handler對象

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.