The method Camera.setParameters() was introduced since Android API level 5. Using this method with parameters.setFlashMode(Parameters.FLASH_MODE_TORCH), we can control the build-in flash light of Android device as a torch.

Modify main.xml to have a button to toggle torch on/off.
main code
Modify AndroidManifest.xml to grant permission of "android.permission.CAMERA".
Download the files.

Modify main.xml to have a button to toggle torch on/off.
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
android:id="@+id/flashcontrol"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
main code
package com.exercise.AndroidFlashLight;
import android.app.Activity;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class AndroidFlashLight extends Activity {
Camera camera = null;
Parameters parameters;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button FlashLightControl = (Button)findViewById(R.id.flashcontrol);
FlashLightControl.setText("Set FLASH_MODE_TORCH");
FlashLightControl.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(camera == null){
camera = Camera.open();
parameters = camera.getParameters();
parameters.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(parameters);
FlashLightControl.setText("Set FLASH_MODE_OFF");
}else{
parameters.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(parameters);
camera.release();
camera = null;
FlashLightControl.setText("Set FLASH_MODE_TORCH");
}
}});
}
}
Modify AndroidManifest.xml to grant permission of "android.permission.CAMERA".
package="com.exercise.AndroidFlashLight"
android:versionCode="1"
android:versionName="1.0">
android:label="@string/app_name">