Android LED Torch App

The most popular android torch apps ask for tons of ridiculous permissions. They want network access, memory access and just about every permission available. The ONLY permission actually needed is camera access. It’s very worrying to grant so many permissions to a simple torch app. Who knows what it’s doing behind the scenes? Thus I never downloaded one and decided to just write my own instead. After all, how hard could it be?

A search for “android torch app development” led to this article on Android Hive. The code is a bit old so one of the alert dialog functions has been deprecated. I also skipped the whole part about adding in sound effects and used a built in Switch instead of trying to do a fancy image switch.

I used the instructions on handling click events on the Toggle Button page instead of the alternative of adding an onCheckedChangeListener as suggested by the Android Hive tutorial.

That seemed simple enough. The program crashed when I tried it on the emulator, I didn’t debug it but guessed it was due to the lack of a torch on the emulator. The alert should have come up but the code was deprecated so perhaps API 19 doesn’t even have it. I might be wrong about this. Anyway, I loaded it onto my device, andddd nothing happened 😦

According to this StackOverflow thread, the flash won’t work for some devices without the SurfaceView and SurfaceHolder components. This other thread gives a lot more detail on how to implement it. The answer is complete, but the line “Your activity needs to implement SurfaceHolder.Callback” literally means something in code. Not obvious on an initial copy and paste.

public class MainActivity extends Activity implements SurfaceHolder.Callback {

Note the addition of implements SurfaceHolder.Callback
Without this bit, the function to add a callback to the SurfaceHolder will fail as the this object is the wrong type. This thread contributed to the required inspiration.

Another thing I had to do was to remove the camera.release() line to stop the program crashing with a null pointer exception when the flashlight is switched off then on again.

Here’s a screenshot of this very simple app.



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s