@Override public void run() { android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_URGENT_AUDIO); Modulation.initDecoder(); // init demodulator Modulation.initProcess(); recdata = new short[recBufSize / 2]; while (!Thread.interrupted() && !mStopRecording) { if (recorder == null) { break; } nread = recorder.read(recdata, 0, recBufSize / 2); if (nread > 0) { retval = Modulation.process(recdata, nread); if (retval == 2) { String str = ""; byte[] result = Modulation.getResult(); try { str = new String(result, "UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } if (mStopRecording) { continue; } Message msg = mHandler.obtainMessage(); msg.what = mEventId; msg.obj = str; mHandler.sendMessage(msg); try { // when receive a message, sleep a little while; // so the main thread has a chance to stop recording immediately Thread.sleep(200); } catch (InterruptedException e) { continue; } } } } try { recorder.stop(); recorder.release(); } catch (Exception e) { e.printStackTrace(); } recorder = null; Modulation.releaseDecoder(); }