日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android子线程更新UI的方法总结

發布時間:2023/12/18 Android 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android子线程更新UI的方法总结 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

消息機制,對于Android開發者來說,應該是非常熟悉。對于處理有著大量交互的場景,采用消息機制,是再好不過了。有些特殊的場景,比如我們都知道,在Android開發中,子線程不能更新UI,而主線程又不能進行耗時操作,一種常用的處理方法就是,在子線程中進行耗時操作,完成之后發送消息,通知主線程更新UI。或者使用異步任務,異步任務的實質也是對消息機制的封裝。

  關于子線程到底能不能更新UI這個問題,之前看到一篇文章很有趣,讓我對這個問題也有了新的認識,那么我也來寫個簡單例子測試下,布局文件如下:

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"tools:context="com.example.joy.messagetest.MainActivity"><TextViewandroid:id="@+id/tv_test"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerHorizontal="true"android:text="Hello World!" /></RelativeLayout>

  布局中只有一個TextView,java代碼如下:

package com.example.joy.messagetest;import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.TextView;public class MainActivity extends AppCompatActivity {private TextView mTvTest;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);initView();new Thread(new Runnable() {@Overridepublic void run() {mTvTest.setText("子線程可以更新UI");}}).start();}private void initView() {mTvTest = (TextView) findViewById(R.id.tv_test);} }

  代碼也很簡單,我開啟子線程,在子線程中,將 TextView 內容設置為“子線程可以更新UI”,而在布局文件中,TextView 的 text 為“Hello world!”,那么現在運行程序,可能會出現的結果有三種:

  • 程序崩了,拋異常了:說明子線程不能更新UI
  • 程序正常運行,textview 上面顯示“Hello World!”:說明子線程不能更新UI
  • 程序正常運行,textview 上面顯示“子線程可以更新UI”:說明子線程可以更新UI

  運行程序,結果如下:

                      

  這說明什么?從結果看,子線程更新UI成功了。真的是這樣嗎?我自己也不相信,趕緊再驗證一遍。這次我在布局文件中添加一個Button,修改后的布局文件如下:

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"tools:context="com.example.joy.messagetest.MainActivity"><TextViewandroid:id="@+id/tv_test"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerHorizontal="true"android:text="Hello World!" /><Buttonandroid:id="@+id/btn_test1"android:layout_below="@id/tv_test"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerHorizontal="true"android:text="子線程更新UI測試"/></RelativeLayout>

  同時修改java代碼:

package com.example.joy.messagetest;import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView;public class MainActivity extends AppCompatActivity implements View.OnClickListener {private TextView mTvTest;private Button mBtnTest1;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);initView();new Thread(new Runnable() {@Overridepublic void run() {mTvTest.setText("子線程可以更新UI");}}).start();}private void initView() {mTvTest = (TextView) findViewById(R.id.tv_test);mBtnTest1 = (Button) findViewById(R.id.btn_test1);mBtnTest1.setOnClickListener(this);}@Overridepublic void onClick(View v) {switch(v.getId()){case R.id.btn_test1:new Thread(new Runnable() {@Overridepublic void run() {mTvTest.setText("子線程真的可以更新UI嗎?");}}).start();break;default:break;}} }

  我們增加了一個button,點擊button,啟動一個子線程,在子線程中將 textview 的顯示內容改為 “子線程真的可以更新UI嗎?”。同樣按照前面的分析,我們再來驗證一下。重新運行程序, textview 顯示 “子線程可以更新UI”, 然后我們點擊 button。結果如下:

                    

  怎么回事?程序崩了。仔細看,你會發現,點擊 button 后 textview 的內容其實是發生了更改的,然后程序崩潰了。查看日志,拋出如下異常:

AndroidRuntime: FATAL EXCEPTION: Thread-176Process: com.example.joy.messagetest, PID: 11201android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6357)at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:874)

  這次終于看到了熟悉的錯誤日志,只有初始創建視圖的線程才能觸碰這些視圖,也就是說只有主線程才能更新UI。通過下面一行

at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6357)

我們能發現點端倪:在 framework/base/core/java/android/view/ViewRootImpl.java 中有一個方法 checkThread ,源碼如下:

void checkThread() {if (mThread != Thread.currentThread()) {throw new CalledFromWrongThreadException("Only the original thread that created a view hierarchy can touch its views.");} }

  該異常就是在這里觸發的。對于這個問題,如果你還想深入下去探究清楚,可以跟進去 RTFSC ! 這里推薦一篇文章,Android中子線程真的不能更新UI嗎?

  說了這么多,其實子線程是不能直接更新UI的。Android實現View更新有兩組方法,分別是invalidate和postInvalidate。前者在UI線程中使用,后者在非UI線程即子線程中使用。換句話說,在子線程調用 invalidate 方法會導致線程不安全。熟悉View工作原理的人都知道,invalidate 方法會通知 view 立即重繪,刷新界面。作一個假設,現在我用 invalidate 在子線程中刷新界面,同時UI線程也在用 invalidate 刷新界面,這樣會不會導致界面的刷新不能同步?這就是invalidate不能在子線程中使用的原因。

  但是我們可以在子線程執行某段代碼,需要更新UI的時候去通知主線程,讓主線程來更新。如何做呢?常見的方法,除了前面提到的在UI線程創建Handler,在子線程發送消息到UI線程,通知UI線程更新UI,還有 handler.post(Runnable r)、 view.post(Runnable r)、activity.runOnUIThread(Runnable r)等方法。跟進去看源碼,發現其實它們的實現原理都還是一樣,最終都是通過Handler發送消息來實現的。下面分別用這幾種方法實現一下在子線程更新UI。

  修改后的布局文件代碼如下:

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"tools:context="com.example.joy.messagetest.MainActivity"><TextViewandroid:id="@+id/tv_test"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerHorizontal="true"android:text="Hello World!" /><Buttonandroid:id="@+id/btn_test1"android:layout_below="@id/tv_test"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerHorizontal="true"android:text="子線程更新UI測試"/><Buttonandroid:id="@+id/btn_test2"android:layout_below="@id/btn_test1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerHorizontal="true"android:text="Handler發送消息"/><Buttonandroid:id="@+id/btn_test3"android:layout_below="@id/btn_test2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerHorizontal="true"android:text="Handler.Post"/><Buttonandroid:id="@+id/btn_test4"android:layout_below="@id/btn_test3"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerHorizontal="true"android:text="View.Post"/><Buttonandroid:id="@+id/btn_test5"android:layout_below="@id/btn_test4"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerHorizontal="true"android:text="Activity.RunOnUIThread"/></RelativeLayout>

?  java代碼如下:

package com.example.joy.messagetest;import android.os.AsyncTask; import android.os.Handler; import android.os.Looper; import android.os.Message; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; import android.widget.Toast;public class MainActivity extends AppCompatActivity implements View.OnClickListener {private TextView mTvTest;private Button mBtnTest1;private Button mBtnTest2;private Button mBtnTest3;private Button mBtnTest4;private Button mBtnTest5;private Handler mHandler = new Handler() {@Overridepublic void handleMessage(Message msg) {super.handleMessage(msg);if (msg.what == 100) {mTvTest.setText("由Handler發送消息");}}};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);initView();new Thread(new Runnable() {@Overridepublic void run() {mTvTest.setText("子線程可以更新UI");}}).start();}private void initView() {mTvTest = (TextView) findViewById(R.id.tv_test);mBtnTest1 = (Button) findViewById(R.id.btn_test1);mBtnTest2 = (Button) findViewById(R.id.btn_test2);mBtnTest3 = (Button) findViewById(R.id.btn_test3);mBtnTest4 = (Button) findViewById(R.id.btn_test4);mBtnTest5 = (Button) findViewById(R.id.btn_test5);mBtnTest1.setOnClickListener(this);mBtnTest2.setOnClickListener(this);mBtnTest2.setOnClickListener(this);mBtnTest3.setOnClickListener(this);mBtnTest4.setOnClickListener(this);mBtnTest5.setOnClickListener(this);}@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.btn_test1:new Thread(new Runnable() {@Overridepublic void run() {mTvTest.setText("子線程真的可以更新UI嗎?");}}).start();break;case R.id.btn_test2: //通過發送消息new Thread(new Runnable() {@Overridepublic void run() {mHandler.sendEmptyMessage(100);}}).start();break;case R.id.btn_test3: //通過Handler.post方法new Thread(new Runnable() {@Overridepublic void run() {mHandler.post(new Runnable() {@Overridepublic void run() {mTvTest.setText("handler.post");}});}}).start();break;case R.id.btn_test4: //通過 view.post方法new Thread(new Runnable() {@Overridepublic void run() {mTvTest.post(new Runnable() {@Overridepublic void run() {mTvTest.setText("view.post");}});}}).start();break;case R.id.btn_test5: //通過 activity 的 runOnUiThread方法new Thread(new Runnable() {@Overridepublic void run() {runOnUiThread(new Runnable() {@Overridepublic void run() {mTvTest.setText("runOnUIThread");}});}}).start();break;default:break;}} }

  運行一下效果如下圖:

                    

  以上就是消息機制最常見的應用場景——在子線程通知主線程更新UI的幾種用法。

 

?

?

?

  

作者:SharpCJ

出處:https://www.cnblogs.com/joy99/p/6121280.html

總結

以上是生活随笔為你收集整理的Android子线程更新UI的方法总结的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。