1.开子线程做耗时:
private HandlerThread mHandlerThread;
private Handler mHandler;
private Runnable mClearCacheRunnable = new Runnable() {
@Override
public void run() {
xxx耗时操作;
}
};
mHandlerThread = new HandlerThread(“xxxName”);
mHandlerThread.start();
mHandler = new Handler(mHandlerThread.getLooper());
mHandler.post(mClearCacheRunnable);
如果Handler中有多个重复耗时操作的话,可以统一在handlemessage中处理:
然后可以构造一个CustomHandler对象进行sendmessage:
mHandler = new
CustomHandler(mHandlerThread.getLooper());
mHandler.sendMessage(0x00);
2.开子线程做ui改动:
private HandlerThread mHandlerThread;
private Handler mHandler;
private Runnable mClearCacheRunnable = new Runnable() {
@Override
public void run() {
ui修改(非耗时操作!); }
};
mHandlerThread = new HandlerThread(Looper.getMainLooper());
mHandlerThread.start();
mHandler = new Handler(mHandlerThread.getLooper());
mHandler.post(mClearCacheRunnable);