這篇文章主要為大家展示了“Android子線程與更新UI的示例分析”,內容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領大家一起研究并學習一下“Android子線程與更新UI的示例分析”這篇文章吧。
在新吳等地區(qū),都構建了全面的區(qū)域性戰(zhàn)略布局,加強發(fā)展的系統(tǒng)性、市場前瞻性、產品創(chuàng)新能力,以專注、極致的服務理念,為客戶提供成都網站設計、網站制作 網站設計制作定制開發(fā),公司網站建設,企業(yè)網站建設,品牌網站設計,成都全網營銷推廣,成都外貿網站建設公司,新吳網站建設費用合理。
引子:
情形1
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView textView = findViewById(R.id.home_tv); ImageView imageView = findViewById(R.id.home_img); new Thread(new Runnable() { @Override public void run() { textView.setText("更新TextView"); imageView.setImageResource(R.drawable.img); } }).start(); }
運行結果:正常運行!??!
情形二
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView textView = findViewById(R.id.home_tv); ImageView imageView = findViewById(R.id.home_img); new Thread(new Runnable() { @Override public void run() { try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } textView.setText("更新TextView"); imageView.setImageResource(R.drawable.img); } }).start(); }
運行結果:異常
android.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)
at android.view.View.requestLayout(View.java:17476)
at android.view.View.requestLayout(View.java:17476)
at android.view.View.requestLayout(View.java:17476)
at android.view.View.requestLayout(View.java:17476)
at android.view.View.requestLayout(View.java:17476)
at android.view.View.requestLayout(View.java:17476)
at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:360)
at android.view.View.requestLayout(View.java:17476)
at android.widget.TextView.checkForRelayout(TextView.java:6871)
at android.widget.TextView.setText(TextView.java:4057)
at android.widget.TextView.setText(TextView.java:3915)
at android.widget.TextView.setText(TextView.java:3890)
at com.dong.demo.MainActivity$1.run(MainActivity.java:44)
at java.lang.Thread.run(Thread.java:818)
不是說,子線程不能更新UI嗎,為什么情形一可以正常運行,情形二不能正常運行呢;
子線程修改UI出現異常,與什么方法有關
首先從出現異常的log日志入手,發(fā)現出現異常的方法調用順序如下:
TextView.setText(TextView.java:4057)
TextView.checkForRelayout(TextView.java:6871)
View.requestLayout(View.java:17476)
RelativeLayout.requestLayout(RelativeLayout.java:360)
View.requestLayout(View.java:17476)
ViewRootImpl.requestLayout(ViewRootImpl.java:874)
ViewRootImpl.checkThread(ViewRootImpl.java:6357)
更改ImageView時,出現的異常類似;
首先看TextView.setText()方法的源碼
private void setText(CharSequence text, BufferType type, boolean notifyBefore, int oldlen) { //省略其他代碼 if (mLayout != null) { checkForRelayout(); } sendOnTextChanged(text, 0, oldlen, textLength); onTextChanged(text, 0, oldlen, textLength); //省略其他代碼
然后,查看以下checkForRelayout()方法的與源碼。
private void checkForRelayout() { // If we have a fixed width, we can just swap in a new text layout // if the text height stays the same or if the view height is fixed. if ((mLayoutParams.width != LayoutParams.WRAP_CONTENT //省略代碼 // We lose: the height has changed and we have a dynamic height. // Request a new view layout using our new text layout. requestLayout(); invalidate(); } else { // Dynamic width, so we have no choice but to request a new // view layout with a new text layout. nullLayouts(); requestLayout(); invalidate(); } }
checkForReLayout方法,首先會調用需要改變的View的requestLayout方法,然后執(zhí)行invalidate()重繪操作;
TextView沒有重寫requestLayout方法,requestLayout方法由View實現;
查看RequestLayout方法的源碼:
public void requestLayout() { //省略其他代碼 if (mParent != null && !mParent.isLayoutRequested()) { mParent.requestLayout(); } if (mAttachInfo != null && mAttachInfo.mViewRequestingLayout == this) { mAttachInfo.mViewRequestingLayout = null; } }
View獲取到父View(類型是ViewParent,ViewPaerent是個接口,requestLayout由子類來具體實現),mParent,然后調用父View的requestLayout方法,比如示例中的父View就是xml文件的根布局就是RelativeLayout。
@Override public void requestLayout() { super.requestLayout(); mDirtyHierarchy = true; }
繼續(xù)跟蹤super.requestLayout()方法,即ViewGroup沒有重新,即調用的是View的requestLayout方法。
經過一系列的調用ViewParent的requestLayout方法,最終調用到ViewRootImp的requestLayout方法。ViewRootImp實現了ViewParent接口,繼續(xù)查看ViewRootImp的requestLayout方法源碼。
@Override public void requestLayout() { if (!mHandlingLayoutInLayoutRequest) { checkThread(); mLayoutRequested = true; scheduleTraversals(); } }
ViewRootImp的requestLayout方法中有兩個方法:
一、checkThread,檢查線程,源碼如下
void checkThread() { if (mThread != Thread.currentThread()) { throw new CalledFromWrongThreadException( "Only the original thread that created a view hierarchy can touch its views."); } }
判斷當前線程,是否是創(chuàng)建ViewRootImp的線程,而創(chuàng)建ViewRootImp的線程就是主線程,當前線程不是主線程的時候,就拋出異常。
二、scheduleTraversals(),查看源碼:
void scheduleTraversals() { if (!mTraversalScheduled) { mTraversalScheduled = true; mTraversalBarrier = mHandler.getLooper().getQueue().postSyncBarrier(); mChoreographer.postCallback( Choreographer.CALLBACK_TRAVERSAL, mTraversalRunnable, null); if (!mUnbufferedInputDispatch) { scheduleConsumeBatchedInput(); } notifyRendererOfFramePending(); pokeDrawLockIfNeeded(); } }
查看mTraversalRunnable中run()方法的具體操作
final class TraversalRunnable implements Runnable { @Override public void run() { doTraversal(); } }
繼續(xù)追蹤doTraversal()方法
void doTraversal() { if (mTraversalScheduled) { mTraversalScheduled = false; mHandler.getLooper().getQueue().removeSyncBarrier(mTraversalBarrier); if (mProfile) { Debug.startMethodTracing("ViewAncestor"); } performTraversals(); if (mProfile) { Debug.stopMethodTracing(); mProfile = false; } } }
查看到performTraversals()方法,熟悉了吧,這是View繪制的起點。
總結一下:
1.Android更新UI會調用View的requestLayout()方法,在requestLayout方法中,獲取ViewParent,然后調用ViewParent的requestLayout()方法,一直調用下去,直到調用到ViewRootImp的requestLayout方法;
2.ViewRootImp的requetLayout方法,主要有兩部操作一個是checkThread()方法,檢測線程,一個是scheduleTraversals,執(zhí)行繪制相關工作;
情形3
@Override protected void onCreate(Bundle savedInstanceState) { Log.i("Dong", "Activity: onCreate"); super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); new Thread(new Runnable() { @Override public void run() { Looper.prepare(); try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } Toast.makeText(MainActivity.this, "顯示Toast", Toast.LENGTH_LONG).show(); Looper.loop(); } }).start(); }
運行結果:正常
分析
下面從Toast源碼進行分析:
public static Toast makeText(Context context, CharSequence text, @Duration int duration) { return makeText(context, null, text, duration); }
makeText方法調用了他的重載方法,繼續(xù)追蹤
public static Toast makeText(@NonNull Context context, @Nullable Looper looper, @NonNull CharSequence text, @Duration int duration) { Toast result = new Toast(context, looper); LayoutInflater inflate = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); View v = inflate.inflate(com.android.internal.R.layout.transient_notification, null); TextView tv = (TextView)v.findViewById(com.android.internal.R.id.message); tv.setText(text); result.mNextView = v; result.mDuration = duration; return result; }
新建了一個Toast對象,然后對顯示的布局、內容、時長進行了設置,并返回Toast對象。
繼續(xù)查看new Toast()
的源碼
public Toast(@NonNull Context context, @Nullable Looper looper) { mContext = context; mTN = new TN(context.getPackageName(), looper); mTN.mY = context.getResources().getDimensionPixelSize( com.android.internal.R.dimen.toast_y_offset); mTN.mGravity = context.getResources().getInteger( com.android.internal.R.integer.config_toastDefaultGravity); }
繼續(xù)查看核心代碼 mTN = new TN(context.getPackageName(), looper);
TN初始化的源碼為:
TN(String packageName, @Nullable Looper looper) { //省略部分不相關代碼 if (looper == null) { // 沒有傳入Looper對象的話,使用當前線程對應的Looper對象 looper = Looper.myLooper(); if (looper == null) { throw new RuntimeException( "Can't toast on a thread that has not called Looper.prepare()"); } } //初始化了Handler對象 mHandler = new Handler(looper, null) { @Override public void handleMessage(Message msg) { switch (msg.what) { case SHOW: { IBinder token = (IBinder) msg.obj; handleShow(token); break; } case HIDE: { handleHide(); // Don't do this in handleHide() because it is also invoked by // handleShow() mNextView = null; break; } case CANCEL: { handleHide(); // Don't do this in handleHide() because it is also invoked by // handleShow() mNextView = null; try { getService().cancelToast(mPackageName, TN.this); } catch (RemoteException e) { } break; } } } }; }
繼續(xù)追蹤handleShow(token)方法:
public void handleShow(IBinder windowToken) { //省略部分代碼 if (mView != mNextView) { // remove the old view if necessary handleHide(); mView = mNextView; Context context = mView.getContext().getApplicationContext(); String packageName = mView.getContext().getOpPackageName(); if (context == null) { context = mView.getContext(); } mWM = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE); /* ·*省略設置顯示屬性的代碼 ·*/ if (mView.getParent() != null) { if (localLOGV) Log.v(TAG, "REMOVE! " + mView + " in " + this); mWM.removeView(mView); } = try { mWM.addView(mView, mParams); trySendAccessibilityEvent(); } catch (WindowManager.BadTokenException e) { /* ignore */ } } }
通過源碼可以看出,Toast顯示內容是通過mWM(WindowManager類型)的直接添加的,更正:mWm.addView 時,對應的ViewRootImp初始化發(fā)生在子線程,checkThread方法中的mThread != Thread.currentThread()
判斷為true,所以不會拋出只能在主線程更新UI的異常。
以上是“Android子線程與更新UI的示例分析”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注創(chuàng)新互聯行業(yè)資訊頻道!
網站欄目:Android子線程與更新UI的示例分析
網站地址:http://jinyejixie.com/article32/ppppsc.html
成都網站建設公司_創(chuàng)新互聯,為您提供移動網站建設、品牌網站建設、微信小程序、建站公司、做網站、搜索引擎優(yōu)化
聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯