/** * 子类在使用handleWorkerMessage前需要调用initWorkerHandler方法来初始化mWorkerHandler和mHandlerThread对象 * * @param name */ public void initWorkerHandler(String name) { if (mHandlerThread == null && mWorkerHandler == null) { mHandlerThread = new HandlerThread(name); mHandlerThread.start(); mWorkerHandler = new WorkerHandler(mHandlerThread.getLooper(), this); } else { logE("initWorkerHandler is called ,don't called again!"); } }
@Override public void onCreate() { super.onCreate(); HandlerThread thread = new HandlerThread(ScheduleUpdaterService.class.getSimpleName()); thread.start(); mServiceLooper = thread.getLooper(); mServiceHandler = new ServiceHandler(mServiceLooper); }
/** Shut down worker thread */ private void shutdownWorker() { if (mWorkerThread != null) { mWorkerThread.getLooper().quit(); mWorkerHandler = null; mWorkerThread = null; } }
@Override public void process() throws Exception { log.trace("process()"); HandlerThread handlerThread = new HandlerThread(); handlerThread.setName(Thread.currentThread().getName() + "-soap"); handlerThread.start(); try { // Wait for the request SOAP message to be parsed before we can // start sending stuff. waitForSoapMessage(); // If the handler thread excepted, do not continue. checkError(); // Verify that the client is registered verifyClientStatus(); // Check client authentication mode verifyClientAuthentication(); // If the message is synchronous, start sending proxy message if (!isAsync) { processRequest(); } if (response != null) { sendResponse(); } } catch (Exception e) { if (reqIns != null) { reqIns.close(); } // Let's interrupt the handler thread so that it won't // block forever waiting for us to do something. handlerThread.interrupt(); throw e; } finally { handlerThread.join(); if (response != null) { response.consume(); } } }
public void recycleWorkerHandler() { if (null != mHandlerThread && null != mWorkerHandler) { mHandlerThread.quit(); mWorkerHandler.removeCallbacksAndMessages(null); } }