Logo Icon Logo
A Crowd-sourced Cookbook on Writing Great Android® Apps
GitHub logo Twitter logo OReilly Book Cover Art

A Simple Torch/Flashlight

Published? true
FormatLanguage: WikiFormat

Problem:

You want to use your smart phone as a torch lamp/flashlight when there is no light or a power failure.

Solution:

A simple solution will be to use the camera flash present in our Android device to use it as a torch lamp.

Discussion:

To begin with, let's see the design of the application.

  • Access the Camera object of the phone.
  • Access the parameters of our Camera
  • Get the flashmodes supported by our camera
  • Set the flashlight parameter in ON state to FLASH_MODE_TORCH and FLASH_OFF on OFF state.

The following code implements the logic required for the application. Note that as of 4.0 ICS, the Camera object will not function unless you$ pass it a SurfaceView object; in the layout file, we create a dummy SurfaceView with a layout width and height of 1dp.$

if(context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH)){
			mTorch = (ToggleButton) findViewById(R.id.toggleButton1);
			mTorch.setOnCheckedChangeListener(new OnCheckedChangeListener() {

				@Override
				public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

					try{
						if(cam != null){
							cam = Camera.open();
						}
						camParams = cam.getParameters();
						List<String> flashModes = camParams.getSupportedFlashModes();
						if(isChecked){
							if (flashModes.contains(Parameters.FLASH_MODE_TORCH)) {
								camParams.setFlashMode(Parameters.FLASH_MODE_TORCH);
							}else{
								showDialog(MainActivity.this, FLASH_TORCH_NOT_SUPPORTED);
							}
						}else{
							camParams.setFlashMode(Parameters.FLASH_MODE_OFF);
						}
						cam.setParameters(camParams);
						cam.startPreview();
					}catch (Exception e) {
						e.printStackTrace();
						cam.stopPreview();
						cam.release();
					}
				}
                                surfaceView = (SurfaceView) this.findViewById(R.id.hiddenSurfaceView);
                                surfaceHolder = surfaceView.getHolder();
                                surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
                                surfaceHolder.addCallback(this);
			});
		}else{
			showDialog(MainActivity.this, FLASH_NOT_SUPPORTED);
		}

The basic logic implemented below is as follows:

  • Check for the existence of the flash in the device.
  • Get the camera object and open it to access it.
  • Get the parameters of the captured camera object.
  • Check the supported flash modes available from the current camera object using getSupportedFlashModes().
  • If the toggle state is ON the set the flash mode of the camera as FLASH_MODE_TORCH otherwise set it as FLASH_MODE_OFF .
public void showDialog (Context context, int dialogId){
		switch(dialogId){
		case FLASH_NOT_SUPPORTED:
			builder = new AlertDialog.Builder(context);
			builder.setMessage("Sorry, Your phone does not support Flash")
			.setCancelable(false)
			.setNeutralButton("Close", new OnClickListener() {

				@Override
				public void onClick(DialogInterface dialog, int which) {
					finish();
				}
			});
			alertDialog = builder.create();
			alertDialog.show();
			break;
		case FLASH_TORCH_NOT_SUPPORTED:
			builder = new AlertDialog.Builder(context);
			builder.setMessage("Sorry, Your camera flash does not support torch feature")
			.setCancelable(false)
			.setNeutralButton("Close", new OnClickListener() {

				@Override
				public void onClick(DialogInterface dialog, int which) {
					finish();
				}
			});
			alertDialog = builder.create();
			alertDialog.show();
		}

	}

Download:

The source code for this project can be downloaded from https://github.com/SaketSrivastav/SimpleTorchLight.

Download:

The source code for this project is in the Android Cookbook repository, http://github.com/IanDarwin/Android-Cookbook-Examples/,in the subdirectory SimpleTorchLight.