THIS CONTENT DOWNLOAD SHORTLY

Objective

This application is about turning on and off device flashlight / torchlight with simple touch.

 

 

Step 1 Create New Project

Create a new project in Eclipse call FlashLightDemo

 

Step 2 Add Required Permission

Open your AndroidManifest.xml file and add required permissions.

  • android.permission.CAMERA: Required to access the camera device
  • android.hardware.camera: Required to access camera hardware features
 

Step 3 activity_main.xml

Now open activity_main.xml file located under res >> layout folder and type the following code. This layout file acts as main screen of the application.

<linearlayout android:background="@android:color/darker_gray" android:gravity="center" android:layout_height="match_parent" android:layout_width="match_parent" android:orientation="vertical" tools:context=".MainActivity" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools">

    <imagebutton android:background="@null" android:contentdescription="@null" android:id="@+id/btnSwitch" android:layout_height="wrap_content" android:layout_width="wrap_content" android:src="@drawable/btn_switch_on"></imagebutton>

</linearlayout>

 

Step 4 MainActivity.java file

Open your MainActivity.java file and do the following changes. Here we just declared required variables.

public class MainActivity extends Activity {
    private ImageButton btnSwitch;
    private Camera camera;
    private boolean isFlashOn;
    private boolean hasFlash;
    private Parameters params;
    private MediaPlayer mp;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // flash switch button
        btnSwitch = (ImageButton) findViewById(R.id.btnSwitch);
}
 

Step 5 Detecting whether device supporting flashlight or not

Detecting whether device supporting flashlight or not. Open MainActivity.java and add the following code. In the following code we are checking availability of the flashlight and if the device doesn’t support it, we’ll show alert dialog and close the application by calling finish().

hasFlash = getApplicationContext().getPackageManager()
        .hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
if (!hasFlash) {
    // device doesn't support flash
    // Show alert message and close the application
    AlertDialog alert = new AlertDialog.Builder(MainActivity.this)
            .create();
    alert.setTitle("Error");
    alert.setMessage("Sorry, your device doesn't support flash light!");
    alert.setButton("OK", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            // closing the application
            finish();
        }
    });
    alert.show();
    return;
}
 

Step 6 Turn On Flashlight

Turning on Flashlight

// getting camera parameters
private void getCamera() {
    if (camera == null) {
        try {
            camera = Camera.open();
            params = camera.getParameters();
        } catch (RuntimeException e) {
            Log.e("Camera Error. Failed to Open. Error: ", e.getMessage());
        }
    }
}

To know more about Camera please follow the following link:

/*
* Turning On flash
*/
private void turnOnFlash() {
    if (!isFlashOn) {
        if (camera == null || params == null) {
            return;
        }
        // play sound
        playSound();
        
        params = camera.getParameters();
        params.setFlashMode(Parameters.FLASH_MODE_TORCH);
        camera.setParameters(params);
        camera.startPreview();
        isFlashOn = true;
        
        // changing button/switch image
        toggleButtonImage();
    }
}
 

Step 7 Turning off Flashlight

private void turnOffFlash() {
    if (isFlashOn) {
        if (camera == null || params == null) {
            return;
        }
        // play sound
        playSound();
        
        params = camera.getParameters();
        params.setFlashMode(Parameters.FLASH_MODE_OFF);
        camera.setParameters(params);
        camera.stopPreview();
        isFlashOn = false;
        
        // changing button/switch image
        toggleButtonImage();
    }
}
 

Step 8 Call On/Off Flash Light Methods

Finally call these functions in button click event

btnSwitch.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        if (isFlashOn) {
            // turn off flash
            turnOffFlash();
        } else {
            // turn on flash
            turnOnFlash();
        }
    }
});

If you have any query or suggestions please comment below. Download full project from github link.

Got an Idea of Android App Development? What are you still waiting for? Contact us now and see the Idea live soon. Our company has been named as one of the best Android App Development Company in India.

I am professional Android application developer with experience of one and half years in Android Application Development. Android has attracted me by its open source platform and the ability to deliver high performance mobile applications.