The update of the UI main thread control completes the asynchronous task of this activity

  SingleMessageView used in the project, after the control is instantiated, click on the user's avatar, then jump to UserInfo to view the user's avatar, user name, signature, label.
  
  Before, my brother wrote a click response of the avatar in SingleMessage, clicked on the user's avatar, passed the user's user name to UserInfo through intent, and at the same time executed an asynchronous task that wanted the server to obtain the user's signature and label. Pass it through the same intent, then UserInfo's onResume () function receives the intent to update the main thread's UI layout. The source code is as follows:
  
  bubuko.com, Bubu button
  
  // other user avatar response
  
  private ImageView.OnClickListener userHeadImgViewListener = new ImageView .OnClickListener () {
  
  @Override
  
  public void onClick (View v) {
  
  String userNameTemp = users_name;
  
  String otheruserData [] = {""};
  
  otheruserData [0] = userNameTemp; // Get the user name
  
  // establish a network link, execute the request Signed and tagged asynchronous tasks
  
  ConnectivityManager connMgr = (ConnectivityManager) mContext.getSystemService (Context.CONNECTIVITY_SERVICE);
  
  NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
  
  if (networkInfo != null && networkInfo.isConnected()){
  
  asyncWebkForOtherinfo = new webTaskForOtherinfo();
  
  if (android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.GINGERBREAD_MR1) {
  
  asyncWebkForOtherinfo.executeOnExecutor(
  
  webTaskForOtherinfo.THREAD_POOL_EXECUTOR, otheruserData);
  
  } else {
  
  asyncWebkForOtherinfo.execute(otheruserData);
  
  }
  
  Log.e("otherinfodown","有网!");
  
  }else {
  
  Log.e("otherinfodown","没有网络连接,请联网后重试...");
  
  }
  
  // TODO Auto-generated method stub
  
  ARUtils.own_information=false;
  
  Intent intent = new Intent (SingleMessageView.this.getContext (), UserInfoActivity.class);
  
  intent.putExtra ("name", users_name);
  
  intent.putExtra ("label", othersignature); // Get the label returned by the asynchronous task and Signature
  
  intent.putExtra ("sign", otherlabel);
  
  String head_img_url = getAppPath () + "/ user_portrait /" + users_name + ". Jpg";
  
  intent.putExtra ("head_img_url", head_img_url);
  
  intent.setFlags (Intent.FLAG_ACTIVITY_NEW_TASK) ; // The flag is used to distinguish which activity passed intent
  
  SingleMessageView.this.getContext (). StartActivity (intent);
  
  Log.v ("head_img_click", "success");
  
  }
  
  };
  
  bubuko.com, Bubu Buckle
  
  in onResume () of userInfo:
  
  bubuko.com,Cloth button
  
  // Get the intent username, avatar path, label, signature in onResume
  
  Intent intent = getIntent ();
  
  String other_head_img_url = intent.getStringExtra ("head_img_url");
  
  String name = intent.getStringExtra ("name");
  
  String label = intent.getStringExtra ("label");
  
  String sign = intent.getStringExtra ("sign");
  
  userNameText. setText (name);
  
  signatureConent.setText (label);
  
  myTab.setText (sign);
  
  Bitmap smallBitmap = ARUtils.loadBitmapFile (other_head_img_url, 48, 48);
  
  userImgButton.setImageBitmap (smallBitmap);
  
  bubuko.com, Bubuko
  
  debugging result is : The signature and label obtained in singleMessage cannot be obtained from the intent. . . According to normal logic, thinking of performing asynchronous tasks, assign values ​​to the global variables sign and label, and then return to the user's avatar response method, and then perform the intent pass operation. The reason why the logic does not run normally is that the asynchronous task is a Very free class, you restrict others too much freedom. . .
  
  The execution time and return of the asynchronous task are not under the control of the ui main thread. The main thread will not obediently wait for the execution of the asynchronous task, and then execute the statements below the asynchronous task, so that the meaning of the asynchronous task is no longer there. .
  
  Therefore, the operation of updating the ui main thread must be placed in the post method of the asynchronous task. What should the main thread do, since you have used the asynchronous task, let the asynchronous task finish what it has done, and, this asynchronous The position of the task must be placed in the same activity as the main UI thread!
  
  So, in the end, our solution is to click on the user's avatar in singleMessage, pass the user name through the intent, and jump to userInfo at the same time, in the onResume () of userInfo, after receiving the intent, execute the custom otherInfo (othername ) Method, the otherInfo (String othername) method, execute the asynchronous task webTaskForOtherINfo (); pass the user name to the asynchronous task, and then define the webTaskForOtherINfo () class in UserInfo, execute the request tag and signature task from the server, and post the function Perform the update of the update UI control in ok. . . . . .

Guess you like

Origin www.cnblogs.com/aquariusunny/p/12729879.html