pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

Android子線程與更新UI問題的深入講解

首先和其他許多的GUI庫一樣,Android的UI線程是不安全的。所以下面這篇文章主要給大家介紹了關(guān)于Android子線程與更新UI問題的相關(guān)資料,需要的朋友可以參考借鑒,下面隨著小編來一起

前言

在Android項(xiàng)目中經(jīng)常有碰到這樣的問題,在子線程中完成耗時(shí)操作之后要更新UI,下面就自己經(jīng)歷的一些項(xiàng)目總結(jié)一下更新的方法。話不多說了,來一起看看詳細(xì)的介紹吧

引子:

情形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)

不是說,子線程不能更新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沒有重寫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由子類來具體實(shí)現(xiàn)),mParent,然后調(diào)用父View的requestLayout方法,比如示例中的父View就是xml文件的根布局就是RelativeLayout。


 @Override
 public void requestLayout() {
 super.requestLayout();
 mDirtyHierarchy = true;
 }

繼續(xù)跟蹤super.requestLayout()方法,即ViewGroup沒有重新,即調(diào)用的是View的requestLayout方法。

經(jīng)過一系列的調(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í)長進(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) {
    // 沒有傳入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 */
    }
   }
  }

通過源碼可以看出,Toast顯示內(nèi)容是通過mWM(WindowManager類型)的直接添加的,更正:mWm.addView 時(shí),對(duì)應(yīng)的ViewRootImp初始化發(fā)生在子線程,checkThread方法中的mThread != Thread.currentThread()判斷為true,所以不會(huì)拋出只能在主線程更新UI的異常。

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對(duì)html5模板網(wǎng)的支持。

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

這篇文章主要介紹了Android開發(fā)之TabHost選項(xiàng)卡及相關(guān)疑難解決方法,結(jié)合實(shí)例形式較為詳細(xì)的分析了Android開發(fā)中TabHost選項(xiàng)卡的常見用法以及相關(guān)疑難問題解決方法,需要的朋友可以參考下
這篇文章主要介紹了Android TabHost選項(xiàng)卡標(biāo)簽圖標(biāo)始終不出現(xiàn)的解決方法,涉及Android界面布局相關(guān)屬性與狀態(tài)設(shè)置操作技巧,需要的朋友可以參考下
這篇文章主要介紹了Android開發(fā)之Notification手機(jī)狀態(tài)欄通知用法,結(jié)合實(shí)例形式分析了Android Notification手機(jī)狀態(tài)欄通知的常見函數(shù)、功能及使用技巧,需要的朋友可以參考下
這篇文章主要介紹了Android開發(fā)實(shí)現(xiàn)模仿微信小窗口功能,結(jié)合實(shí)例形式分析了Android實(shí)現(xiàn)微信風(fēng)格Dialog對(duì)話框窗口相關(guān)功能與布局操作技巧,需要的朋友可以參考下
這篇文章主要介紹了Android開發(fā)之PopupWindow創(chuàng)建彈窗、對(duì)話框的方法,結(jié)合實(shí)例形式詳細(xì)分析了Android使用PopupWindow創(chuàng)建對(duì)話框相關(guān)操作技巧,需要的朋友可以參考下
這篇文章主要介紹了Android開發(fā)之DatePickerDialog、TimePickerDialog時(shí)間日期對(duì)話框用法,結(jié)合實(shí)例形式分析了Android使用DatePickerDialog、TimePickerDialog顯示日期時(shí)間相關(guān)操作技巧,需要的朋友可以參考
主站蜘蛛池模板: 安全光栅|射频导纳物位开关|音叉料位计|雷达液位计|两级跑偏开关|双向拉绳开关-山东卓信机械有限公司 | 不发火防静电金属骨料_无机磨石_水泥自流平_修补砂浆厂家「圣威特」 | 哈尔滨京科脑康神经内科医院-哈尔滨治疗头痛医院-哈尔滨治疗癫痫康复医院 | 爆炸冲击传感器-无线遥测传感器-航天星百科| FAG轴承,苏州FAG轴承,德国FAG轴承-恩梯必传动设备(苏州)有限公司 | 杭州中策电线|中策电缆|中策电线|杭州中策电缆|杭州中策电缆永通集团有限公司 | 吊篮式|移动式冷热冲击试验箱-二槽冷热冲击试验箱-广东科宝 | vr安全体验馆|交通安全|工地安全|禁毒|消防|安全教育体验馆|安全体验教室-贝森德(深圳)科技 | 液压升降平台_剪叉式液压/导轨式升降机_传菜机定做「宁波日腾升降机厂家」 | 琉璃瓦-琉璃瓦厂家-安徽盛阳新型建材科技有限公司 | 广州/东莞小字符喷码机-热转印打码机-喷码机厂家-广州瑞润科技 | 涡轮流量计_LWGY智能气体液体电池供电计量表-金湖凯铭仪表有限公司 | 润滑脂-高温润滑脂-轴承润滑脂-食品级润滑油-索科润滑油脂厂家 | 物流公司电话|附近物流公司电话上门取货 | 天津中都白癜风医院_天津白癜风医院_天津治疗白癜风 | 河北凯普威医疗器材有限公司,高档轮椅系列,推车系列,座厕椅系列,协步椅系列,拐扙系列,卫浴系列 | 冷水机-冰水机-冷冻机-冷风机-本森智能装备(深圳)有限公司 | 数控走心机-双主轴走心机厂家-南京建克| 机床导轨_导轨板_滚轮导轨-上海旻佑精密机械有限公司 | 欧美日韩国产一区二区三区不_久久久久国产精品无码不卡_亚洲欧洲美洲无码精品AV_精品一区美女视频_日韩黄色性爱一级视频_日本五十路人妻斩_国产99视频免费精品是看4_亚洲中文字幕无码一二三四区_国产小萍萍挤奶喷奶水_亚洲另类精品无码在线一区 | 999范文网_优质范文下载写作帮手 | 仿古瓦,仿古金属瓦,铝瓦,铜瓦,铝合金瓦-西安东申景观艺术工程有限公司 | 爱佩恒温恒湿测试箱|高低温实验箱|高低温冲击试验箱|冷热冲击试验箱-您身边的模拟环境试验设备技术专家-合作热线:400-6727-800-广东爱佩试验设备有限公司 | 活性炭厂家-蜂窝活性炭-粉状/柱状/果壳/椰壳活性炭-大千净化-活性炭 | LNG鹤管_内浮盘价格,上装鹤管,装车撬厂家-连云港赛威特机械 | 国资灵活用工平台_全国灵活用工平台前十名-灵活用工结算小帮手 | 高温链条油|高温润滑脂|轴承润滑脂|机器人保养用油|干膜润滑剂-东莞卓越化学 | 冷凝锅炉_燃气锅炉_工业燃气锅炉改造厂家-北京科诺锅炉 | 「钾冰晶石」氟铝酸钾_冰晶石_氟铝酸钠「价格用途」-亚铝氟化物厂家 | 杭州中策电线|中策电缆|中策电线|杭州中策电缆|杭州中策电缆永通集团有限公司 | 河南卓美创业科技有限公司-河南卓美防雷公司-防雷接地-防雷工程-重庆避雷针-避雷器-防雷检测-避雷带-避雷针-避雷塔、机房防雷、古建筑防雷等-山西防雷公司 | 雪花制冰机(实验室雪花制冰机)百科 | 酒万铺-酒水招商-酒水代理| 合同书格式和范文_合同书样本模板_电子版合同,找范文吧 | 雪花制冰机(实验室雪花制冰机)百科 | 硫酸钡厂家_高光沉淀硫酸钡价格-河南钡丰化工有限公司 | 猎头招聘_深圳猎头公司_知名猎头公司 | 哈希PC1R1A,哈希CA9300,哈希SC4500-上海鑫嵩实业有限公司 | 机器视觉检测系统-视觉检测系统-机器视觉系统-ccd检测系统-视觉控制器-视控一体机 -海克易邦 | CXB船用变压器-JCZ系列制动器-HH101船用铜质开关-上海永上船舶电器厂 | 航空连接器,航空插头,航空插座,航空接插件,航插_深圳鸿万科 |