Android UI編程(5)——Looper

來源:互聯網
上載者:User

標籤:looper   thread   message   handler   messagequeue   

Looper通常是運行在一個訊息的迴圈隊列中的這個線程中,線程預設不會提供一個迴圈的訊息去關聯它們,即在一般的線程中是沒有一個訊息佇列去關聯這個訊息的。那麼如果線程想管理這些訊息,就必須在此線程中調用Looper.prepare()使這個訊息佇列運行起來,並且調用Looper.loop()這個方法使它訊息佇列一直運行到停止。而Handler就是訊息佇列一個互動訊息,包括從將訊息發到訊息佇列,以及從訊息佇列取出訊息並處理。

總結

Android使用Message對象來管理訊息資料,並將這些Message對象發到訊息佇列中進行管理。Message對象放入訊息佇列以及從訊息佇列取出並處理,這些操作都是通過Handler對象來管理的。但是執行這些機制的最外層是通過Looper對象進行管理的。如所示:


Message:訊息,其中包含了訊息ID,訊息處理對象以及處理的資料等,由MessageQueue統一隊列,終由Handler處理

Handler:處理者,負責Message的發送及處理。使用Handler時,需要實現handleMessage(Message msg)方法來對特定的Message進行處理,例如更新UI等

MessageQueue:訊息佇列,用來存放Handler發送過來的訊息,並按照FIFO規則執行。當然,存放Message並非實際意義的儲存,而是將Message以鏈表的方式串聯起來的,等待Looper的抽取

Looper:訊息泵,不斷地從MessageQueue中抽取Message執行。因此,一個MessageQueue需要一個Looper,針對接收Message訊息線程

Thread:線程,負責調度整個訊息迴圈,即訊息迴圈的執行場所

Android系統的訊息佇列和訊息迴圈都是針對具體的線程的,一個線程可以存在(當然也可以不存在)一個訊息佇列和一個訊息迴圈(Looper),特定線程的訊息只能分發給本線程,不能進行跨線程、跨進程通訊的。並且建立的背景工作執行緒預設是沒有訊息迴圈和訊息佇列的,如果想讓線程具有隊列和訊息迴圈,需要線上程中首先調用Looper.prepare()來建立訊息佇列,然後調用Looper.loop()進入訊息迴圈。

然而,Activity是一個UI線程,運行於主線程中。Android系統在啟動的時候會為Activity建立一個訊息佇列和訊息迴圈(Looper)。Android應用程式進程在啟動的時候,會在進程中載入ActivityThread類,並且執行這個類的main方法,應用程式的訊息迴圈過程就是在這個main方法裡面實現的,即UI線程預設有個Looper對象,在Activity有一個預設的Looper的對象,來處理子線程中發送的訊息

例子一

AndroidManifest.xml——沒有做任何修改,建立工程預設產生的

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.wxl.handler_message"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="8"        android:targetSdkVersion="18" />    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <activity            android:name="com.wxl.looper.MainActivity"            android:label="@string/app_name" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>    </application></manifest>
activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:gravity="center"    android:orientation="vertical"    tools:context=".MainActivity" >    <TextView        android:id="@+id/textView"        android:layout_width="wrap_content"        android:layout_height="wrap_content" />        <Button         android:id="@+id/button"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="發送hello world"/></LinearLayout>
MainActivity.java

package com.wxl.looper;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.view.View;import android.widget.Button;import android.widget.TextView;import android.app.Activity;public class MainActivity extends Activity {private TextView textView;private Button button;private MyThread thread;private Handler handler;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        textView = (TextView)this.findViewById(R.id.textView);        button = (Button)this.findViewById(R.id.button);        button.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubMessage message = Message.obtain();message.what = 1;message.obj = "hello world";handler.sendMessage(message);}});        thread = new MyThread();    }        public class MyThread extends Thread    {    public MyThread() {// TODO Auto-generated constructor stub    handler = new Handler(){    @Override    public void handleMessage(Message msg) {    // TODO Auto-generated method stub    super.handleMessage(msg);    if (1 == msg.what)    {    textView.setText(""+msg.obj);    }    }    };}    @Override    public void run() {    // TODO Auto-generated method stub    super.run();    }    }        }
點擊按鈕前

點擊按鈕後

此例子還不能看出Looper的作用,因為採用的Activity預設的Looper。從上述例子也可以看出,我並沒有啟動MyThread線程

例子二

修改MainActivity.java檔案

package com.wxl.looper;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.view.View;import android.widget.Button;import android.widget.TextView;import android.app.Activity;public class MainActivity extends Activity {private TextView textView;private Button button;private MyThread thread;private Handler handler;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        textView = (TextView)this.findViewById(R.id.textView);        button = (Button)this.findViewById(R.id.button);        button.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubMessage message = Message.obtain();message.what = 1;message.obj = "hello world";handler.sendMessage(message);}});        thread = new MyThread();        thread.start();    }        public class MyThread extends Thread    {    public MyThread() {// TODO Auto-generated constructor stub//    handler = new Handler(){//    @Override//    public void handleMessage(Message msg) {//    // TODO Auto-generated method stub//    super.handleMessage(msg);//    if (1 == msg.what)//    {//    textView.setText(""+msg.obj);//    }//    }//    };}    @Override    public void run() {    // TODO Auto-generated method stub    super.run();    handler = new Handler(){    @Override    public void handleMessage(Message msg) {    // TODO Auto-generated method stub    super.handleMessage(msg);    if (1 == msg.what)    {    textView.setText(""+msg.obj);    }    }    };    }    }        }
將handler的初始化方法線程的run方法中,在Oncreate()方法啟動MyThread線程。此時運行程式出現了以下錯誤提示:

在子線程中不能建立函數,沒有調用Looper.prepare()。為什麼上述例子將handler的初始化放在MyThread構造器中就沒有問題呢,而放在run方法中就問題呢?原因是,上述在MyThread構造器中去初始化handler仍然屬於UI主線程的代碼,通過我們並沒有啟動MyThread線程也知道。但是在run方法中初始化handler是屬於MyThread子線程中,在子線程中執行個體化,脫離了UI主線程,而且該線程預設不提供Looper。我們修改MyThread線程中的run方法,修改如下:

public void run() {    // TODO Auto-generated method stub    super.run();    Looper.prepare();    handler = new Handler(){    @Override    public void handleMessage(Message msg) {    // TODO Auto-generated method stub    super.handleMessage(msg);    if (1 == msg.what)    {    //textView.setText(""+msg.obj);子線程不能更新UI    Log.i("", ""+msg.obj);    }    }    };    Looper.loop();    }

點擊按鈕




Android UI編程(5)——Looper

聯繫我們

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