工作线程(耗时操作)与UI线程实现异步更新
概述:工作线程A(多任务)执行期间,把单一任务的结果返回到UI线程更新。
实现: 创建基础流程类(两个handler)
public abstract class BaseQueueThread
{
/**
* 设置处理线程、ui处理流程,由子类实现
*/
protected abstract Object doInWork(int integer);//耗时操作 protected abstract void doInUi(Object obj);//更新ui
private Handler mWorkHandler,mUiHandler;
private boolean isDestroy;
private PriorityQueue<Message> mQueue;//优先队列
public BaseQueueThread()
{
initProcess();
} //暴露数据访问接口
public void sendMessage(int priority,Integer obj)
{
Message msg = Message.obtain();
msg.what = priority;
msg.obj = obj;
mWorkHandler.sendMessage(msg);//从工作线程开始
} private void initProcess()
{
mUiHandler = new Handler(Looper.getMainLooper());
HandlerThread handlerThread = new HandlerThread(getClass().getName());
handlerThread.start();
mWorkHandler = new Handler(handlerThread.getLooper())
{
private boolean isWait;//是否在等待ui更新
@Override
public void handleMessage(Message msg)
{
if(isDestroy)
{
return;
}
try
{
if(msg.what == -1)
{
if (mQueue == null)
{
isWait = false;
return;
}
msg = mQueue.poll();
if (msg == null)
{
isWait = false;
return;
} }else if(isWait)
{
if (mQueue == null)
{
mQueue = new PriorityQueue<Message>(11, new Comparator<Message>()
{
@Override
public int compare(Message lhs, Message rhs)
{
return rhs.what - lhs.what;
}
});
}
Message message = Message.obtain();
message.copyFrom(msg);
mQueue.add(message);
return;
}else
{
isWait = true;
} final Object result = doInWork((Integer) msg.obj);
mUiHandler.post(new Runnable()
{
@Override
public void run()
{
doInUi(result);
Message msg1 = Message.obtain();
msg1.what = -1;//通知ui更新成功
mWorkHandler.sendMessageAtFrontOfQueue(msg1);
}
});
} catch (Exception e)
{
isWait = false;
e.printStackTrace();
}
}
}; }
public void onDestroy()
{
isDestroy = true;
if(mWorkHandler!=null)
{
mWorkHandler.removeCallbacksAndMessages(null);
} if (mUiHandler!=null)
{
mUiHandler.removeCallbacksAndMessages(null);
}
} }
具体实现类:
public class EasyQueueThread extends BaseQueueThread
{
private TextView mTextView;
public EasyQueueThread(TextView textView)
{
super();
mTextView = textView;
} @Override
protected Object doInWork(int obj)
{
try
{
Thread.sleep(1000);
} catch (InterruptedException e)
{
e.printStackTrace();
}
return "ok"+obj;
} @Override
protected void doInUi(Object obj)
{
mTextView.setText((String)obj);
}
}
发送工作线程消息,逐步更新UI:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView bt = (TextView) findViewById(R.id.button1); EasyQueueThread easyQueueThread = new EasyQueueThread(bt);
for (int i = 0;i< 10 ;i++)
{
easyQueueThread.sendMessage(i,i);
} }
}
3.总结:耗时操作不要放在UI线程,关键点要处理好什么时候执行工作线程,什么时候执行UI线程。
本文章转载自: