Android中子線程和UI線程通信詳解
Android中子線程和UI線程之間通信的詳細解釋
1.在多線程編程這塊,我們經(jīng)常要使用Handler,Thread和Runnable這三個類,那么他們之間的關(guān)系你是否弄清楚了呢?下面詳解一下。
2.首先在開發(fā)Android應(yīng)用時必須遵守單線程模型的原則:
Android UI操作并不是線程安全的并且這些操作必須在UI線程中執(zhí)行。
3.Handler:
(1).概念:
Handler是溝通Activity 與Thread/runnable的橋梁。而Handler是運行在主UI線程中的,它與子線程可以通過Message對象來傳遞數(shù)據(jù)。
(2).使用:
A:Handler是運行在UI線程中,主要接收子線程發(fā)送的數(shù)據(jù)信息, 并用此數(shù)據(jù)配合主線程更新UI,用來跟UI主線程交互用。比如可以用handler發(fā)送一個message,然后在handler的線程中來接收、處理該消息。
B:消息的處理者。通過Handler對象我們可以封裝Message對象,然后通過sendMessage(msg)把Message對象添加到MessageQueue中;當(dāng)MessageQueue循環(huán)到該Message時,就會調(diào)用該Message對象對應(yīng)的handler對象的handleMessage()方法對其進行處理。
C:Handler可以分發(fā)Runnable對象,也可以分發(fā)Message對象。
4.Message:
消息對象,顧名思義就是記錄消息信息的類。也就是說是信息的載體,存放信息內(nèi)容。這個類有幾個比較重要的字段:
(1).arg1和arg2:我們可以使用兩個字段用來存放我們需要傳遞的整型值,在Service中,我們可以用來存放Service的ID。
(2).obj:該字段是Object類型,我們可以讓該字段傳遞某個對象到消息的接受者中。
(3).what:這個字段可以說是消息的標志,判斷是接收了哪個消息。在消息處理中,我們可以根據(jù)這個字段的不同的值進行不同的處理,類似于我們在處理Button事件時,通過switch(v.getId())判斷是點擊了哪個按鈕。
Android推薦通過Message.obtain()或者Handler.obtainMessage()獲取Message對象。這并不一定是直接創(chuàng)建一個新的實例,而是先從消息池中看有沒有可用的Message實例,存在則直接取出并返回這個實例。反之如果消息池中沒有可用的Message實例,則根據(jù)給定的參數(shù)new一個新Message對象。通過分析源碼可得知,Android系統(tǒng)默認情況下在消息池中實例化10個Message對象。
5.源碼展示:
(1).activity_main.xml布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:id="@+id/btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="自定義Thread繼承Thread" /> <Button android:id="@+id/btn2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="自定義Runnable實現(xiàn)Runnable" /> <Button android:id="@+id/btn3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="定時更新UI界面,Handler分發(fā)Runnable對象" /> <Button android:id="@+id/btn4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="定時更新UI界面,Handler分發(fā)Message對象" /> <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="0" /> </LinearLayout>
(2).MainActivity.java
package com.chengdong.su.threaddemo; import com.chengdong.su.threaddemo.util.MyRunnable; import com.chengdong.su.threaddemo.util.MyThread; import android.R.integer; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; public class MainActivity extends Activity implements OnClickListener { /** TAG */ private final String TAG = getClass().getSimpleName(); /** the object of the button */ private Button mButton; /** the object of the button */ private Button mButton2; /** the object of the button */ private Button mButton3; /** the object of the button */ private Button mButton4; /** the object of the TextView */ private TextView mTextView; /** 計數(shù) */ private int mCount = 0; /** 標志 */ private int MESSAGE_FLAG = 1; /** * Handler分發(fā)Runnable對象的方式 */ private Handler mHandler = new Handler(); Runnable runnable = new Runnable() { @Override public void run() { mCount++; mHandler.postDelayed(runnable, 1000); mTextView.setText(mCount + ""); } }; /*** * Handler分發(fā)Message對象的方式 */ Handler mHandler2 = new Handler() { public void handleMessage(android.os.Message msg) { if (msg.what == 1) { mTextView.setText("Handler分發(fā)Message對象的方式"); } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); } /** * 初始化組件對象 */ private void initView() { mButton = (Button) findViewById(R.id.btn); mButton2 = (Button) findViewById(R.id.btn2); mButton3 = (Button) findViewById(R.id.btn3); mButton4 = (Button) findViewById(R.id.btn4); mButton.setOnClickListener(this); mButton2.setOnClickListener(this); mButton3.setOnClickListener(this); mButton4.setOnClickListener(this); mTextView = (TextView) findViewById(R.id.tv); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.btn: { // 方法一:繼承的方式:自定義Thread繼承Thread,開啟一個新的線程 new MyThread().start(); break; } case R.id.btn2: { // 方法二:實現(xiàn)的方式:implement Runnable new Thread(new MyRunnable()).start(); break; } // 方法三:handler分發(fā)Runnable對象:定時更新UI界面 提交計劃任務(wù)馬上執(zhí)行 case R.id.btn3: { // Handler分發(fā)Runnable對象 mHandler.post(runnable); break; } // 方法四:Handler分發(fā)Message對象 ,定時更新UI界面 提交計劃任務(wù)馬上執(zhí)行 case R.id.btn4: { // 不推薦這種方式 // Message msg = new Message(); // 推薦使用這種獲取對象的方式:從消息池中獲得可用的Message對象 Message msg = Message.obtain(); msg.what = MESSAGE_FLAG; mHandler2.sendMessage(msg); break; } default: break; } } }
(3).MyRunnable.java
package com.chengdong.su.threaddemo.util; import android.util.Log; /*** * 自定義一個MyRunnable線程 * * @author scd * */ public class MyRunnable implements Runnable { public MyRunnable() { super(); } /** TAG */ private final String TAG = getClass().getSimpleName(); @Override public void run() { for (int i = 0; i < 20; i++) { Log.e(TAG, Thread.currentThread().getName() + ",實現(xiàn)的方法" + i); } } }
(4)MyThread.java
package com.chengdong.su.threaddemo.util; import android.util.Log; /*** * 自定義一個線程 * * @author scd * */ public class MyThread extends Thread { public MyThread() { super(); } /** TAG */ private final String TAG = getClass().getSimpleName(); @Override public void run() { super.run(); for (int i = 0; i < 10; i++) { Log.e(TAG, Thread.currentThread().getName() + ",繼承Thread類:" + i); } } }
相關(guān)文章
Android自定義View葉子旋轉(zhuǎn)完整版(六)
這篇文章主要為大家詳細介紹了Android自定義View葉子旋轉(zhuǎn)完整版,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-03-03Android基礎(chǔ)之使用Fragment適應(yīng)不同屏幕和分辨率(分享)
以下是對Fragment的使用進行了詳細的分析介紹,需要的朋友可以過來參考下2013-07-07android studio編譯jar包或者aar包的方法教程詳解
這篇文章主要介紹了android studio編譯jar包或者aar包的方法教程,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-03-03Android系統(tǒng)的五種數(shù)據(jù)存儲形式實例(二)
Android系統(tǒng)有五種數(shù)據(jù)存儲形式,分別是文件存儲、SP存儲、數(shù)據(jù)庫存儲、contentprovider 內(nèi)容提供者、網(wǎng)絡(luò)存儲。本文介紹了Android系統(tǒng)的五種數(shù)據(jù)存儲形式,有興趣的可以了解一下。2016-12-12android?ViewPager實現(xiàn)一個無限輪播圖
大家好,本篇文章主要講的是android?ViewPager實現(xiàn)一個無限輪播圖,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下2022-02-02