企业建立网站需要什么条件,做海报的话网站,东莞型网站建设,广州手机app开发本节向你展示如何在任务中发送数据给UI线程里的对象#xff0c;这个特性允许你在后台线程工作#xff0c;完了在UI线程展示结果。在UI线程定义一个HandlerHandler是Android系统线程管理框架里的一部分。一个Handler对象接收消息#xff0c;并且运行代码来处理消息。正常情况…本节向你展示如何在任务中发送数据给UI线程里的对象这个特性允许你在后台线程工作完了在UI线程展示结果。在UI线程定义一个HandlerHandler是Android系统线程管理框架里的一部分。一个Handler对象接收消息并且运行代码来处理消息。正常情况下你为新线程创建Handler,但你也可以为已有的线程创建一个Handler.当你连接Handler到UI线程时处理消息的代码会在UI线程上运行.在创建线程池的类的构造器里实例化Handler对象保存在全局变量里。用Handler(Looper)方法实例化连接到UI线程构造方法使用Looper对象也是Android系统线程管理框架里的一部分.Looper类有一个静态方法getMainLooper()可以获取UI线程的Looper对象。如:private PhotoManager() {...// Defines a Handler object thats attached to the UI threadmHandler new Handler(Looper.getMainLooper()) {...在Handler里覆盖handleMessage()。Android系统会在Handler管理的线程收到新消息时调用该方法。一个指定线程的所有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对象里接下来把任务对象和状态码传给Handler对象。在这个对象里发送一个包含状态 和任务对象的消息给Handler.因为Handler在UI线程上运行它可以移动数据给UI对象。在任务对象里存储数据如这是一个Runnable,运行在后台线程它解析Bitmap,并保存到它的父对象。Runnable同时保存状态码DECODE_STATE_COMPLETED。// A class that decodes photo files into Bitmapsclass 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还包含一个ImageView引用用来显示Bitmap.尽管引用Bitmap和ImageView是在同一个对象里但因为不是在UI线程你不能直接让ImageView显示Bitmap.沿对象层次逐级发送状态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);}...}移动数据到UIPhotoManager从PhotoTask对象接收到状态码和PhotoTask对象的句柄。因为状态是TASK_COMPLETE创建一个包含状态和任务对象的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()为每个进来的Message检查状态码。如果状态码是TASK_COMPLETE任务就是完成了Message里的PhotoTask对象包含Bitmap和ImageView.因为Handler.handleMessage()运行在UI线程它可以安全地为ImageView设置Bitmap.