2

I am working on Turning ON/OFF Camera flashlights.

What i want to do is :

  1. When user clicks on Start LED button, it should start the Camera LED.
  2. Stop camera LED automatically after n Seconds eg. after 30seconds (Thats totally based on user settings).
  3. Stop Camera LED when user presses "Stop LED" Button

I have used ToggleButton for Starting and Stopping Camera LED.

Here is what i have done :

 toggleLED.setOnClickListener(new OnClickListener() {  
            public void onClick(View v) {      

                if (toggleLED.isChecked())
                {
                    startLED();
                }
                else      
                {
                    stopLED();
                }
            }
        });

So far i got success in turning ON and OFF Camera Lights.

For Turning On :

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) {
                }
     });
}

For Turning OFF :

void stopLED() {
    cam.stopPreview();
    cam.release();
}

ToggleButton's function is working fine.

But what i want is : How can i shut off Camera Lights after n Seconds ?

Edited:

I tried using this & it completely works fine..But i want to know whether i am going in a correct way or not..

 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();
                }
            }
        });      
Kartik Domadiya
  • 29,868
  • 19
  • 93
  • 104

2 Answers2

2

Use the AlarmManager to schedule a PendingIntent which will turn the camera light off. By using the AlarmManager you can be sure it will still run even if your application is paused or killed by the system.

You can see some sample code at Android: How to use AlarmManager

Community
  • 1
  • 1
Intrications
  • 16,782
  • 9
  • 50
  • 50
0

I tried using this & it completely works fine..But i want to know whether i am going in a correct way or not..

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();
    }                     
  }
}); 
RajaReddy PolamReddy
  • 22,428
  • 19
  • 115
  • 166
Kartik Domadiya
  • 29,868
  • 19
  • 93
  • 104
  • i am using above code for LED on and off, but after n seconds it goes to off. i tried this in Samsung Ace and Samsung Galaxy SII. what is the problem please help me, i am stuck at this point from 1 week.. – RajaReddy PolamReddy Nov 16 '11 at 04:52
  • I am facing the same problem since 3 months : http://stackoverflow.com/questions/6939816/turn-on-off-camera-led-flash-light-in-samsung-galaxy-ace-2-2-1-galaxy-tab... – Kartik Domadiya Nov 17 '11 at 04:14
  • but, i downloaded one application from market it's working perfectly on the device why like that. – RajaReddy PolamReddy Nov 17 '11 at 04:18
  • @PolamReddyRajaReddy : Dont know.. I have searched alot and still searching but no results yet. – Kartik Domadiya Nov 17 '11 at 04:23