最近在做项目的时候,发觉有很多需要工作线程及消息处理的地方,而android自身提供了很好的包装类,这里对自己使用的一些梳理,权作自己的笔记。
主要有几种实现方式:
1.使用Handler.postAtTime定时将Runnable作为工作线程,处理耗时工作;
2.通过使用HandlerThread;
3.自定义Thread,并实现消息循环。
代码1:Runnable片段
private Handler mTickerHandler = new Handler(); private Runnable mTicker = new Runnable() { public void run() { if (mTickerStop) { return; } if (!mTickerSkip) { // TODO 执行实际的工作 } long time = SystemClock.uptimeMillis(); long step = time - time % 1000L + 1000L * STEP; // time - time % 1000L此处的减去求余操作,是为了将定时设置在整数秒上 mTickerHandler.postAtTime(MainActivity.this.mTicker, step); } };
private final class ServiceHandler extends Handler { public ServiceHandler(Looper looper) { super(looper); } @Override public void handleMessage(Message msg) { int what = msg.what; trace("Thread:" + Thread.currentThread() + ", msg:" + msg); // 执行任务操作 } } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 此处定义了两个工作线程,并将工作线程并设置响应的消息处理的handler HandlerThread senderThread = new HandlerThread("[sender_service]"); senderThread.start(); mSenderServiceLooper = senderThread.getLooper(); mSenderServiceHandler = new ServiceHandler(mSenderServiceLooper); HandlerThread receiverThread = new HandlerThread("[receiver_service]"); receiverThread.start(); mReceiverServiceLooper = receiverThread.getLooper(); mReceiverServiceHandler = new ServiceHandler(mReceiverServiceLooper); // 开启定时处理 startTicker(); } @Override protected void onDestroy() { super.onDestroy(); stopTicker(false); // 销毁时要调用quit方法,将工作线程的消息循环停止 mSenderServiceLooper.quit(); mReceiverServiceLooper.quit(); }
public Handler mExportHandler; private static final int _QUIT = -1; private static final int _STEP = 0; private static final int _START = 1; private static final int _END = 2; private static final int _FAIL = 3;
new Thread() { private int preNext = 0; public void run() { Looper.prepare(); mExportHandler = new Handler() { public void handleMessage(Message msg) { System.out.println("msg=" + msg); switch (msg.what) { case _QUIT: Looper.myLooper().quit(); break; case _STEP: int next = exports(isSender, msg.arg1); if (next > 15) { mExportHandler.sendEmptyMessage(_QUIT); return; } if (preNext == next) { mExportHandler.sendMessageDelayed( mExportHandler.obtainMessage(_STEP, next, 0), 5000L); } else { preNext = next; mExportHandler.sendMessage(mExportHandler .obtainMessage(_STEP, next, 0)); } break; case _START: break; case _END: case _FAIL: mReporter.report( ProcessReport.STATE_BACKUP_CONTACTS, msg.arg1, msg.arg2, msg.obj); break; } } }; mExportHandler.sendMessage(mExportHandler.obtainMessage(_STEP, 0, 0)); Looper.loop(); } }.start();
因为项目中需要分步骤进行处理,在一个步骤上失败后需要进行间隔尝试
next > 15时作退出处理,15只是一个步骤定义,可以指定更多或更少。
private int exports(boolean isSender, int step) { int next = step; try { if (next == 0) { // 步骤一 if (!excutore(...)) { // 失败时next保持不变,直接放回,定时间隔后再次执行 return next; } // 执行成功后,进行下一步操作 next++; } if (next == 1) { // 步骤二 if (!excutore(...)) { // 失败时next保持不变,直接放回,定时间隔后再次执行 return next; } } // 其他步骤依此 ... // > 15则退出循环 next = 16; } catch (Exception e) { AdbDebug.LogException(e); } return next; }
代码1、2的完整代码
import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.HandlerThread; import android.os.Looper; import android.os.Message; import android.os.SystemClock; public class MainActivity extends Activity { private static final boolean DEBUG = false; private static final long STEP = 15L; private boolean mTickerStop = true; private boolean mTickerSkip; private volatile Looper mSenderServiceLooper; private volatile Looper mReceiverServiceLooper; private volatile ServiceHandler mSenderServiceHandler; private volatile ServiceHandler mReceiverServiceHandler; private Handler mTickerHandler = new Handler(); private Runnable mTicker = new Runnable() { public void run() { if (mTickerStop) { return; } if (!mTickerSkip) { // TODO 执行实际的工作 } long time = SystemClock.uptimeMillis(); long step = time - time % 1000L + 1000L * STEP; // time - time % 1000L此处的减去求余操作,是为了将定时设置在整数秒上 mTickerHandler.postAtTime(MainActivity.this.mTicker, step); } }; private final class ServiceHandler extends Handler { public ServiceHandler(Looper looper) { super(looper); } @Override public void handleMessage(Message msg) { int what = msg.what; trace("Thread:" + Thread.currentThread() + ", msg:" + msg); // 执行任务操作 } } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 此处定义了两个工作线程,并将工作线程并设置响应的消息处理的handler HandlerThread senderThread = new HandlerThread("[sender_service]"); senderThread.start(); mSenderServiceLooper = senderThread.getLooper(); mSenderServiceHandler = new ServiceHandler(mSenderServiceLooper); HandlerThread receiverThread = new HandlerThread("[receiver_service]"); receiverThread.start(); mReceiverServiceLooper = receiverThread.getLooper(); mReceiverServiceHandler = new ServiceHandler(mReceiverServiceLooper); // 开启定时处理 startTicker(); } @Override protected void onDestroy() { super.onDestroy(); stopTicker(false); // 销毁时要调用quit方法,将工作线程的消息循环停止 mSenderServiceLooper.quit(); mReceiverServiceLooper.quit(); } private void startTicker() { mTickerSkip = false; if (mTickerStop) { mTickerStop = false; mTicker.run(); } } private void stopTicker(boolean isStop) { if (isStop && !mTickerStop) { // 当前没有停止时,则移除消息队列中的消息,并将mTickerStop赋值true mTickerHandler.removeCallbacks(mTicker); mTickerStop = true; } mTickerSkip = true; } }