這篇文章主要為大家展示了“Android子線程與更新UI的示例分析”,內(nèi)容簡(jiǎn)而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“Android子線程與更新UI的示例分析”這篇文章吧。
在新吳等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強(qiáng)發(fā)展的系統(tǒng)性、市場(chǎng)前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供成都網(wǎng)站設(shè)計(jì)、網(wǎng)站制作 網(wǎng)站設(shè)計(jì)制作定制開(kāi)發(fā),公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),品牌網(wǎng)站設(shè)計(jì),成都全網(wǎng)營(yíng)銷推廣,成都外貿(mào)網(wǎng)站建設(shè)公司,新吳網(wǎng)站建設(shè)費(fèi)用合理。
引子:
情形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(); }
運(yùn)行結(jié)果:正常運(yùn)行?。?!
情形二
@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(); }
運(yùn)行結(jié)果:異常
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)
不是說(shuō),子線程不能更新UI嗎,為什么情形一可以正常運(yùn)行,情形二不能正常運(yùn)行呢;
子線程修改UI出現(xiàn)異常,與什么方法有關(guān)
首先從出現(xiàn)異常的log日志入手,發(fā)現(xiàn)出現(xiàn)異常的方法調(diào)用順序如下:
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時(shí),出現(xiàn)的異常類似;
首先看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方法,首先會(huì)調(diào)用需要改變的View的requestLayout方法,然后執(zhí)行invalidate()重繪操作;
TextView沒(méi)有重寫(xiě)requestLayout方法,requestLayout方法由View實(shí)現(xiàn);
查看RequestLayout方法的源碼:
public void requestLayout() { //省略其他代碼 if (mParent != null && !mParent.isLayoutRequested()) { mParent.requestLayout(); } if (mAttachInfo != null && mAttachInfo.mViewRequestingLayout == this) { mAttachInfo.mViewRequestingLayout = null; } }
View獲取到父View(類型是ViewParent,ViewPaerent是個(gè)接口,requestLayout由子類來(lái)具體實(shí)現(xiàn)),mParent,然后調(diào)用父View的requestLayout方法,比如示例中的父View就是xml文件的根布局就是RelativeLayout。
@Override public void requestLayout() { super.requestLayout(); mDirtyHierarchy = true; }
繼續(xù)跟蹤super.requestLayout()方法,即ViewGroup沒(méi)有重新,即調(diào)用的是View的requestLayout方法。
經(jīng)過(guò)一系列的調(diào)用ViewParent的requestLayout方法,最終調(diào)用到ViewRootImp的requestLayout方法。ViewRootImp實(shí)現(xiàn)了ViewParent接口,繼續(xù)查看ViewRootImp的requestLayout方法源碼。
@Override public void requestLayout() { if (!mHandlingLayoutInLayoutRequest) { checkThread(); mLayoutRequested = true; scheduleTraversals(); } }
ViewRootImp的requestLayout方法中有兩個(gè)方法:
一、checkThread,檢查線程,源碼如下
void checkThread() { if (mThread != Thread.currentThread()) { throw new CalledFromWrongThreadException( "Only the original thread that created a view hierarchy can touch its views."); } }
判斷當(dāng)前線程,是否是創(chuàng)建ViewRootImp的線程,而創(chuàng)建ViewRootImp的線程就是主線程,當(dāng)前線程不是主線程的時(shí)候,就拋出異常。
二、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繪制的起點(diǎn)。
總結(jié)一下:
1.Android更新UI會(huì)調(diào)用View的requestLayout()方法,在requestLayout方法中,獲取ViewParent,然后調(diào)用ViewParent的requestLayout()方法,一直調(diào)用下去,直到調(diào)用到ViewRootImp的requestLayout方法;
2.ViewRootImp的requetLayout方法,主要有兩部操作一個(gè)是checkThread()方法,檢測(cè)線程,一個(gè)是scheduleTraversals,執(zhí)行繪制相關(guān)工作;
情形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(); }
運(yùn)行結(jié)果:正常
分析
下面從Toast源碼進(jìn)行分析:
public static Toast makeText(Context context, CharSequence text, @Duration int duration) { return makeText(context, null, text, duration); }
makeText方法調(diào)用了他的重載方法,繼續(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; }
新建了一個(gè)Toast對(duì)象,然后對(duì)顯示的布局、內(nèi)容、時(shí)長(zhǎng)進(jìn)行了設(shè)置,并返回Toast對(duì)象。
繼續(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) { //省略部分不相關(guān)代碼 if (looper == null) { // 沒(méi)有傳入Looper對(duì)象的話,使用當(dāng)前線程對(duì)應(yīng)的Looper對(duì)象 looper = Looper.myLooper(); if (looper == null) { throw new RuntimeException( "Can't toast on a thread that has not called Looper.prepare()"); } } //初始化了Handler對(duì)象 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); /* ·*省略設(shè)置顯示屬性的代碼 ·*/ 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 */ } } }
通過(guò)源碼可以看出,Toast顯示內(nèi)容是通過(guò)mWM(WindowManager類型)的直接添加的,更正:mWm.addView 時(shí),對(duì)應(yīng)的ViewRootImp初始化發(fā)生在子線程,checkThread方法中的mThread != Thread.currentThread()
判斷為true,所以不會(huì)拋出只能在主線程更新UI的異常。
以上是“Android子線程與更新UI的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
網(wǎng)站欄目:Android子線程與更新UI的示例分析
網(wǎng)站地址:http://bm7419.com/article32/ppppsc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供移動(dòng)網(wǎng)站建設(shè)、品牌網(wǎng)站建設(shè)、微信小程序、建站公司、做網(wǎng)站、搜索引擎優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)