ui主线程控件的更新就让这个activity的异步任务做完整

  项目中使用的SingleMessageView,控件实例化后,点击用户头像,此时跳转到UserInfo里查看这个用户的头像、用户名、签名、标签。
  
  之前,师兄在SingleMessage里写了个头像的点击响应,点击用户头像,把用户的用户名通过intent传给UserInfo,同时执行想服务器获取这个用户的签名和标签的异步任务,把这两个值也通过同一个intent传递过去,然后UserInfo的onResume()函数接收intent,进行主线程ui布局的更新,源代码如下:
  
  bubuko.com,布布扣
  
  //other用户头像响应
  
  private ImageView.OnClickListener userHeadImgViewListener=new ImageView.OnClickListener() {
  
  @Override
  
  public void onClick(View v) {
  
  String userNameTemp =users_name;
  
  String otheruserData[] = { "" };
  
  otheruserData[0] = userNameTemp;//获取用户名
  
  //建立网络链接,执行请求签名和标签的异步任务
  
  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);//获取异步任务返回的标签和签名
  
  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);//标志位用来区分是哪个activity传过来的intent
  
  SingleMessageView.this.getContext().startActivity(intent);
  
  Log.v("head_img_click","success");
  
  }
  
  } ;
  
  bubuko.com,布布扣
  
  在userInfo的onResume()中:
  
  bubuko.com,布布扣
  
  //在onResume中获取intent的用户名、头像路径、标签、签名
  
  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,布布扣
  
  调试结果就是:在singleMessage里获取到的签名和标签,intent怎么都获取不到。。。按照正常的逻辑,以为执行异步任务,把值都赋值给全局变量sign、label,然后会返回到用户头像响应的方法里面,接着执行intent的传值操作,逻辑没有正常运行的原因就是异步任务是个很自由的类,你限制人家自由太多了。。。
  
  异步任务的执行时间和返回,不受ui主线程的控制,主线程不会乖乖的等异步任务执行完,在去接着执行异步任务下面的语句,这样异步任务的意义也就没有木有了。。
  
  所以,更新ui主线程的操作一定要放在异步任务的post方法里面,主线程该干嘛干嘛,既然你用到了异步任务,就让异步任务把该做完的做完,并且,这个异步任务的位置一定要放在与ui主线程同一个activity里面!
  
  所以,最后,我们的解决方法就是在singleMessage里,点击用户头像,把用户名通过intent传过去,同时跳转到userInfo,在userInfo的onResume()里,接收intent后,执行自定义的otherInfo(othername)方法,otherInfo(String othername)这个方法里,执行异步任务webTaskForOtherINfo();把用户名传给异步任务,然后在UserInfo里定义webTaskForOtherINfo()这个类,执行向服务器请求标签、签名任务,在post函数里执行更新UI控件的更新,就ok啦。。。。。。

猜你喜欢

转载自www.cnblogs.com/aquariusunny/p/12729879.html
今日推荐