title: How to keep android camera flash on after screen locked
body: <p>I'm creating a flash light app for android. I'm using service to turn on and off flash. All things are works fine. But when screen locked flash light is automatically turning off and service already running. this happen only device unplugged from charger. when device charging flash light keep turn on after screen locked.How can I avoid this problem.</p>
  
  <p>Here is my <code>FlashLightService.java</code> file</p>
  
  <pre><code>public class FlashLightService extends Service {
      Camera camera;
      Camera.Parameters parameters;
      static boolean isTurnOn;
      static int usedTime = 0;
      static String TAG = "coretorch_service";
  Thread t;
  static String formattedTime;
  
  @Override
  public int onStartCommand(Intent intent, int flags, int startId) {
      Toast.makeText(this, "Torch turned on", Toast.LENGTH_SHORT).show();
      isTurnOn = true;
  
      t =new Thread(new Runnable() {
          @Override
          public void run() {
              while (isTurnOn){
                  try {
                      Thread.sleep(1000);
                  } catch (InterruptedException e) {
                      e.printStackTrace();
                  }
                  usedTime++;
              }
              usedTime = 0;
          }
      });
  
      t.start();
  
      camera = Camera.open();
      parameters = camera.getParameters();
  
      parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
      camera.setParameters(parameters);
      camera.startPreview();
  
      return START_NOT_STICKY;
  }
  
  @Override
  public void onDestroy() {
      parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
      camera.setParameters(parameters);
      camera.stopPreview();
      camera.release();
      isTurnOn = false;
      super.onDestroy();
      Toast.makeText(this, "Torch turned off", Toast.LENGTH_SHORT).show();
  }
  
  @Nullable
  @Override
  public IBinder onBind(Intent intent) {
      return null;
  }
  
  public static boolean getFlashStatus() {
      return isTurnOn;
  }
  
  public static String getUsedTime(){
      formattedTime = toTimeFormat(usedTime);
      return formattedTime;
  }
  
  static String toTimeFormat(int secs){
      String time;
      int mins;
      int seconds;
  
      if (secs &lt; 60){
          time = String.valueOf(secs) + " sec";
      }
      else {
          mins = (secs / 60);
          seconds = (secs - (mins * 60));
          time = String.valueOf(mins) + " mins " + String.valueOf(seconds) + " sec";
      }
  
      return time;
   }
    }
  </code></pre>