0

I am developing a very simple flashlight application, while I have successfully achieved what I was looking for, I would like to perform it that way I want it to. Currently my flashlight remains on while my activity is active, as soon as I hit the home button to minimize the activity flashlight turns off. I want the flashlight to stay on and turn off only when I click the turn off button in my activity.

I also want something like that if the flashlight is active and user hits the home button to minimize the activity, Turn Off button to be displayed in the notification bar.

Please guide me.

android_newbie
  • 667
  • 2
  • 14
  • 32

2 Answers2

0

Please try to use bellow code.

public class CustomFlashLight {

private static CustomFlashLight instance;

private static Camera mCamera;


public static CustomFlashLight getInstance() {
    if (null == instance) {
        instance = new CustomFlashLight();
    }
    return instance;
}

public static boolean checkFlashAvailaibility(Context context) {
    boolean flag = false;
    try {
        flag = context.getPackageManager().hasSystemFeature(
                PackageManager.FEATURE_CAMERA_FLASH);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return flag;
}

public static boolean turnOnLight() {
    boolean flag = false;
    try {
        mCamera = Camera.open();
        if (mCamera != null) {
            Parameters params = mCamera.getParameters();
            if (Build.MODEL.equals("GT-P1000")) {
                params.setFlashMode(Parameters.FLASH_MODE_ON);
            } else {

                params.setFlashMode(Parameters.FLASH_MODE_TORCH);
            }
            mCamera.setParameters(params);
            mCamera.startPreview();
            mCamera.autoFocus(new AutoFocusCallback() {

                public void onAutoFocus(boolean success, Camera camera) {
                }
            });

            flag = true;
        } else {
            flag = false;
        }
    } catch (Exception e) {
        e.printStackTrace();

    }
    return flag;
}

public static boolean turnOffLight() {
    boolean flag = false;
    try {
        if (mCamera != null) {
            mCamera.stopPreview();
            mCamera.release();
            mCamera = null;
            flag = true;
        }
    } catch (Exception e) {
        e.printStackTrace();

    }
    return flag;
}

}

nilesh patel
  • 834
  • 1
  • 5
  • 10
  • 1
    I have successfully implemented the functionality to turn on and off the flash light. The solution I am looking for is to keep flash light on when I minimize the application using home button. – android_newbie Jul 18 '13 at 05:43
  • please add one static flag and set true when Turn on and chech in onStart method when flag is true than call turnOn method . – nilesh patel Jul 18 '13 at 05:50
  • Thank you for sparing your time. I got the solution. – android_newbie Jul 18 '13 at 05:57
0

Got solution.

@Override
public void onBackPressed() {
super.onBackPressed();

params = camera.getParameters();
params.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(params);
camera.stopPreview();
isFlashOn = false;

if (camera!= null) {
    camera.release();
    camera= null;
}
Log.d("Camera","Back Pressed");
}

And removed all the code from onStop() and onPause() method.

android_newbie
  • 667
  • 2
  • 14
  • 32