title: Forcing Camera LED Light to turn OFF after n minutes or n seconds in Android
body: <p>I am working on Turning ON/OFF Camera flashlights.</p>
  
  <p>What i want to do is :</p>
  
  <ol>
  <li>When user clicks on Start LED button, it should start the Camera LED.</li>
  <li>Stop camera LED automatically after n Seconds eg. after 30seconds (Thats totally based on  user settings).</li>
  <li>Stop Camera LED when user presses "Stop LED" Button</li>
  </ol>
  
  <p>I have used ToggleButton for Starting and Stopping Camera LED.</p>
  
  <p>Here is what i have done :</p>
  
  <pre><code> toggleLED.setOnClickListener(new OnClickListener() {  
              public void onClick(View v) {      
  
                  if (toggleLED.isChecked())
                  {
                      startLED();
                  }
                  else      
                  {
                      stopLED();
                  }
              }
          });
  </code></pre>
  
  <p>So far i got success in turning ON and OFF Camera Lights.</p>
  
  <p>For Turning On :</p>
  
  <pre><code>void startLED(){
      Camera cam;
      cam = Camera.open();     
      Parameters params = cam.getParameters();
      params.setFlashMode(Parameters.FLASH_MODE_ON);
      cam.setParameters(params);
      cam.startPreview();
      cam.autoFocus(new AutoFocusCallback() {
                  public void onAutoFocus(boolean success, Camera camera) {
                  }
       });
  }
  </code></pre>
  
  <p>For Turning OFF :</p>
  
  <pre><code>void stopLED() {
      cam.stopPreview();
      cam.release();
  }
  </code></pre>
  
  <p>ToggleButton's function is working fine.</p>
  
  <p>But what i want is :
           How can i shut off Camera Lights after n Seconds ?</p>
  
  <p><strong>Edited:</strong></p>
  
  <p>I tried using this &amp; it completely works fine..But i want to know whether i am going in a correct way or not..</p>
  
  <pre><code> Runnable r = new Runnable()
                {
                    public void run()
                    {
                        stopLED();                  
  
                    }
                };
  
  
  toggleLED.setOnClickListener(new OnClickListener() {  
              public void onClick(View v) {      
                  // Perform action on clicks     
  
  
  
                  if (toggleLED.isChecked())
                  {
                      startLED();                 
  
                      int onledseconds=pref.getInt(MySettings.SHUTOFF_PERIOD,0);                  
  
                      if(onledseconds!=0)
                          handler.postDelayed(r,ledonseconds);
                  }
                  else      
                  {
  
                      stopLED();
                  }
              }
          });      
  </code></pre>