link href='http://alexgorbatchev.com/pub/sh/current/styles/shCore.css' rel='stylesheet' type='text/css'/>

Monday, June 16, 2014

Flashy the Flash Light: a tutorial to make your own flash light app Day 3

I ended the last tutorial saying that there are a few errors in the code that won't allow it to function properly.  Yea, I know, I'm a jerk.  Deal with it :P

1.  One of the problems mysteriously fixed its self.........  When I wrote the original app I was having a problem with the light going off when the screen was rotated.  This is currently not an issue and I have no idea why... hmmmm.... maybe one of you can help me out?

either way......

2. The other issue is when the back or the home button is pressed.  This causes the program to close but the light stays on.  The problem occurs when you go to turn off the light because the toggle button resets.  The result is a crashhhhh.......

There are 2 methods to fix this problem depending on how you want your flashlight to work.  If you WANT the camera to stay on when the app is closed then all you have to do is check if the light is on. Simple right???  It's actually very very easy.  All you have to do is add an if statement in the onCreate() method.

if (ourCam !=null){
			tb.setChecked(true);
		}

DONE!! simple and clean.


The other option is to turn the light off when you close out of the app.  This is also pretty simple and to do it you'll need to use the onStop() method
protected void onStop() {
		// TODO Auto-generated method stub
		if (tb.isChecked()) {
			tb.setChecked(false);
			ourCam.stopPreview();
			ourCam.release();			
		}
		super.onStop();
	}

Add that to your code and you should be all done!!

Hope you enjoyed!

No comments:

Post a Comment