1

I am making an application in which i want to blink the flashlight on incoming call and the application is running fine but the problem is i want when the power button is pressed the flashlight must stop blinking... How could i stop the flashlight when power button is pressed???

if(callState==TelephonyManager.CALL_STATE_RINGING) {
        try {

            releaseCameraAndPreview();
            cam = Camera.open();
            p = cam.getParameters();

            String myString = "010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101011";
            long blinkDelay = 50;


            for (int i = 0; i < myString.length(); i++) {
                //state=intent.getStringExtra(TelephonyManager.EXTRA_STATE);
                callState = mgr.getCallState();

                /*KeyEvent event = null;
                if (event.getKeyCode() == KeyEvent.KEYCODE_POWER) {
                    p.setFlashMode(Parameters.FLASH_MODE_OFF);
                    cam.release();

                }*/

public boolean onKeyDown(int keyCode, KeyEvent event)//this line is giving errors
 {
                    if (keyCode == KeyEvent.KEYCODE_POWER) {
                        p.setFlashMode(Parameters.FLASH_MODE_OFF);
                        cam.release();
                        event.startTracking(); // Needed to track long presses
                        return true;
                    }
                    return super.onKeyDown(keyCode, event);
                };

                if (callState==TelephonyManager.CALL_STATE_IDLE){
                    p.setFlashMode(Parameters.FLASH_MODE_OFF);
                    cam.release();
                    break;                  

                    }else if (callState==TelephonyManager.CALL_STATE_OFFHOOK){
                        p.setFlashMode(Parameters.FLASH_MODE_OFF);
                        cam.release();
                    break;  
                    }               

                if (myString.charAt(i) == '0') {
                    p.setFlashMode(Parameters.FLASH_MODE_TORCH);
                    cam.setParameters(p);
                } else {
                    p.setFlashMode(Parameters.FLASH_MODE_OFF);
                    cam.setParameters(p);
                }

                    Thread.sleep(blinkDelay);


            }
        }catch (Exception e) {
            // TODO: handle exception
            Log.d(tag, "in catch1");
            Log.d(tag, e.toString());

    }
nawaab saab
  • 1,892
  • 2
  • 20
  • 36

1 Answers1

0

You can make broadcast receiver for getting push the power button.
Broadcast catch the push the button, you can off the flashlight.

or

Try this method. I can find this answer.
How to hook into the Power button in Android?

Community
  • 1
  • 1
Amadas
  • 703
  • 1
  • 5
  • 10