当前位置: 首页 > news >正文

个人空间备案网站名称太原网站建

个人空间备案网站名称,太原网站建,四川成都网站优化,手机网站怎么提高关键词原文地址#xff1a;http://android.xsoftlab.net/training/multiple-threads/communicate-ui.html 上节课我们学习了如何启动一项由ThreadPoolExecutor所管理的线程任务。最后这节课我们将学习如何从任务中发送结果数据给UI线程。这项手段可以使任务在执行完毕后将结果显示到…原文地址http://android.xsoftlab.net/training/multiple-threads/communicate-ui.html 上节课我们学习了如何启动一项由ThreadPoolExecutor所管理的线程任务。最后这节课我们将学习如何从任务中发送结果数据给UI线程。这项手段可以使任务在执行完毕后将结果显示到UI中去。 每个APP拥有独立的UI线程。只有在UI线程中创建的对象才可以访问该线程中的其它对象。正因为运行任务的线程不是UI线程所以它们不可以直接访问UI对象。为了将数据从后台线程转移到UI线程需要使用运行在UI线程中的Handler对象。 在UI线程中定义Handler Handler是Android系统框架管理线程的一部分。Handler对象专门用于接收消息处理消息。 一般来说可以为新线程创建一个Handler也可以为一个已经连接好的线程创建Handler。当你将Handler连接到UI线程时处理消息的代码都会运行在UI线程中。 在构建线程池的类的构造方法中实例化一个Handler对象并将该对象的引用存储于全局变量中。通过Handler(Looper)重载构造方法所实例化Handler可以与UI线程产生关联。这个构造方法所使用的Looper参数是Android系统的线程管理框架的另一部分。当以指定的Looper实例初始化一个Handler对象时Handler对象会运行在Looper对象所在的线程中。 private PhotoManager() { ...// Defines a Handler object thats attached to the UI threadmHandler new Handler(Looper.getMainLooper()) {... 在Handler内重写handleMessage()方法。Android系统会在Handler所管理的线程中接收到一条新的消息时回调该方法 /** handleMessage() defines the operations to perform when* the Handler receives a new Message to process.*/Overridepublic void handleMessage(Message inputMessage) {// Gets the image task from the incoming Message object.PhotoTask photoTask (PhotoTask) inputMessage.obj;...}...} } 将数据从非UI线程转移到UI线程 为了将数据从后台进程转移到UI进程首先将数据的引用以及UI对象存储于任务对象中。接下来将该任务对象及状态码传给由Handler对象所初始化的对象。在这个对象内发送一条包含状态码的任务对象给Handler。因为Handler是运行在UI线程的所以它可以将数据交给UI对象。 存储数据于任务对象中 举个例子这里有一个Runnable对象运行于后台线程它用于解码一个Bitmap对象并将其存储于它所属的对象PhotoTask中。该Runnable还会存储一个状态码DECODE_STATE_COMPLETED。 // A class that decodes photo files into Bitmaps class PhotoDecodeRunnable implements Runnable {...PhotoDecodeRunnable(PhotoTask downloadTask) {mPhotoTask downloadTask;}...// Gets the downloaded byte arraybyte[] imageBuffer mPhotoTask.getByteBuffer();...// Runs the code for this taskpublic void run() {...// Tries to decode the image bufferreturnBitmap BitmapFactory.decodeByteArray(imageBuffer,0,imageBuffer.length,bitmapOptions);...// Sets the ImageView BitmapmPhotoTask.setImage(returnBitmap);// Reports a status of completedmPhotoTask.handleDecodeState(DECODE_STATE_COMPLETED);...}... } ... PhotoTask还包含了一个用于展示Bitmap的ImageView的句柄。尽管引用Bitmap以及ImageView的是同一个对象但是还是不能将Bitmap赋值给ImageView因为当前并没有处在UI线程中。 发送状态到对象层级 PhotoTask在层级内处于第二高度。它维护了图像的解码数据以及一个View对象。它会接收PhotoDecodeRunnable中的状态码并将其传给维护线程池以及Handler的那个对象。 public class PhotoTask {...// Gets a handle to the object that creates the thread poolssPhotoManager PhotoManager.getInstance();...public void handleDecodeState(int state) {int outState;// Converts the decode state to the overall state.switch(state) {case PhotoDecodeRunnable.DECODE_STATE_COMPLETED:outState PhotoManager.TASK_COMPLETE;break;...}...// Calls the generalized state methodhandleState(outState);}...// Passes the state to PhotoManagervoid handleState(int state) {/** Passes a handle to this task and the* current state to the class that created* the thread pools*/sPhotoManager.handleState(this, state);}... } 将数据展示到UI PhotoManager收到从PhotoTask中发来的状态码然后处理这个PhotoTask对象。因为状态是TASK_COMPLETE所以先创建一个Message然后通过这个Message对象将状态以及人物对象发送给Handler: public class PhotoManager {...// Handle status messages from taskspublic void handleState(PhotoTask photoTask, int state) {switch (state) {...// The task finished downloading and decoding the imagecase TASK_COMPLETE:/** Creates a message for the Handler* with the state and the task object*/Message completeMessage mHandler.obtainMessage(state, photoTask);completeMessage.sendToTarget();break;...}...} 最后在Handler.handleMessage()中检查每条消息的状态。如果消息的状态为TASK_COMPLETE那么表明该任务已经终结那么Message中所包含的PhotoTask对象包含了一个Bitmap以及一个ImageView。因为Handler.handleMessage()是运行在UI线程的所以它可以安全的将Bitmap赋给ImageView: private PhotoManager() {...mHandler new Handler(Looper.getMainLooper()) {Overridepublic void handleMessage(Message inputMessage) {// Gets the task from the incoming Message object.PhotoTask photoTask (PhotoTask) inputMessage.obj;// Gets the ImageView for this taskPhotoView localView photoTask.getPhotoView();...switch (inputMessage.what) {...// The decoding is donecase TASK_COMPLETE:/** Moves the Bitmap from the task* to the View*/localView.setImageBitmap(photoTask.getImage());break;...default:/** Pass along other messages from the UI*/super.handleMessage(inputMessage);}...}...}...} ... }
http://www.huolong8.cn/news/240659/

相关文章:

  • 桂林论坛网站有哪些前端工程师招聘
  • wordpress资讯站免费软件看小说上门龙婿叶辰
  • 宁波网站推广方式定制公司怎么把文章导入wordpress
  • 族谱网站建设禁止百度收录的网站
  • 宁波快速建站公司icp网站备案系统
  • 辽宁智能建站系统价格如何做本地门户网站
  • 怎样建立自己的销售网站织梦小说网站
  • 做彩票网站是违法的吗品牌英语
  • 移动网站建设模板淄博网站制作服务
  • 佛山市品牌网站建设价格设计网站公司的账务处理
  • 番禺区移动端网站制作H5 网站
  • 罗湖商城网站建设哪家服务周到培训心得
  • .案例 商务网站的推广策略常见的导航网站有哪些
  • 电商网站设计欣赏阿里云oss可以做网站
  • 西安微动免费做网站学院网站建设
  • 快件网站建设郑州高端建站
  • 将网站发布到微信小程序怎么做wordpress文章发布函数
  • 朝阳市做网站网站建设系统开发感想与收获
  • 推荐西安知名的集团门户网站建设公司wordpress点击显示微信二维码
  • 企业建站系统模板苏州微网站建设
  • 建设网站总结报告漳州做网站配博大钱少a
  • 一级a做爰网站网络优化工资一般多少
  • 自己做网站可以揽业务吗设计前沿的网站
  • 公司网站模板 html长沙seo推广营销
  • 国外网站建设发展现状wordpress的结构
  • 信息发布的网站怎么做泉州哪里做网站开发
  • 官方网站welcome扬中话
  • 做ppt选小图案的网站网站空间和服务器有什么区别
  • 怎么知道自己的网站被k东坑仿做网站
  • 手机网站 jsp一键生成app的软件