Code example for HandlerThread

Methods: getLooper, start

0
	/** 
	 * This method initializes the aysnc screenshot saving logic 
	 */ 
	private void initScreenShotSaver() { 
		if(screenShotSaverThread == null || screenShotSaver == null) {
			screenShotSaverThread = new HandlerThread("ScreenShotSaver");
			screenShotSaverThread.start();
			screenShotSaver = new ScreenShotSaver(screenShotSaverThread);
		} 
	} 
 
	/**  
	 * This is the thread which causes a screenshot sequence to happen 
	 * in parallel with testing. 
	 */ 
	private class ScreenshotSequenceThread extends Thread {
		private int seqno = 0;
 
		private String name;
		private int quality;
		private int frameDelay;
		private int maxFrames;
 
		private boolean keepRunning = true;
 
		public ScreenshotSequenceThread(String _name, int _quality, int _frameDelay, int _maxFrames) {
			name = _name;
			quality = _quality; 
			frameDelay = _frameDelay;
			maxFrames = _maxFrames;
		} 
 
		public void run() { 
			while(seqno < maxFrames) {
				if(!keepRunning || Thread.interrupted()) break;
				doScreenshot(); 
				seqno++;
				try { 
					Thread.sleep(frameDelay);
				} catch (InterruptedException e) {
				} 
			} 
			screenshotSequenceThread = null;
		} 
 
		public void doScreenshot() { 
			View v = getScreenshotView();
			if(v == null) keepRunning = false;
			String final_name = name+"_"+seqno;
			ScreenshotRunnable r = new ScreenshotRunnable(v, final_name, quality);
			Log.d(LOG_TAG, "taking screenshot "+final_name);
			activityUtils.getCurrentActivity(false).runOnUiThread(r);
		} 
 
		public void interrupt() { 
			keepRunning = false;
			super.interrupt(); 
		} 
	} 
 
	/** 
	 * Here we have a Runnable which is responsible for taking the actual screenshot, 
	 * and then posting the bitmap to a Handler which will save it. 
	 * 
	 * This Runnable is run on the UI thread. 
	 */ 
	private class ScreenshotRunnable implements Runnable {
 
		private View view;
		private String name;
		private int quality;
 
		public ScreenshotRunnable(final View _view, final String _name, final int _quality) {
			view = _view;
			name = _name;
			quality = _quality;
		} 
 
		public void run() { 
			if(view !=null){
				Bitmap  b;
 
				if(view instanceof WebView){
					b = getBitmapOfWebView((WebView) view);
				} 
				else{ 
					b = getBitmapOfView(view);
				} 
				if(b != null)
					screenShotSaver.saveBitmap(b, name, quality);
				else  
					Log.d(LOG_TAG, "NULL BITMAP!!");
			} 
		} 
	} 
 
	/** 
	 * This class is a Handler which deals with saving the screenshots on a separate thread. 
	 * 
	 * The screenshot logic by necessity has to run on the ui thread.  However, in practice 
	 * it seems that saving a screenshot (with quality 100) takes approx twice as long 
	 * as taking it in the first place.  
	 * 
	 * Saving the screenshots in a separate thread like this will thus make the screenshot 
	 * process approx 3x faster as far as the main thread is concerned. 
	 * 
	 */ 
	private class ScreenShotSaver extends Handler {
		public ScreenShotSaver(HandlerThread thread) {
			super(thread.getLooper());
		} 
 
		/** 
		 * This method posts a Bitmap with meta-data to the Handler queue. 
		 *