简体   繁体   English

android屏幕唤醒锁定时如何通过音量按钮打开闪光灯?

[英]how to open flash light through volume button when screen wake lock in android?

hi guys i am developing an app in which i want to open the camera flash light through volume button when phone in wake lock screen state or display is off. 嗨,大家好,我正在开发一个应用程序,当手机处于唤醒锁定屏幕状态或显示屏关闭时,我想通过音量按钮打开相机闪光灯。 i am also do this through service class and broadcast receiver but not right output. 我也通过服务类和广播接收器来执行此操作,但输出不正确。 i think anyone understand my problem. 我认为有人明白我的问题。 is Anybody have solution so please help me. 是任何人都有解决方案,所以请帮助我。 Thanks in Advances. 预先感谢。 i am using this code app work properly but i want to do this when phone in wake-lock state or display is off. 我正在使用此代码应用程序正常工作,但是当手机处于唤醒锁定状态或显示屏关闭时,我想这样做。

public boolean dispatchKeyEvent(KeyEvent event) {
    int action = event.getAction();
    int keyCode = event.getKeyCode();

        switch (keyCode) {
        case KeyEvent.KEYCODE_VOLUME_UP:
            if (action == KeyEvent.ACTION_DOWN) {
                count++;
                if(count==2){

                    Parameters p = camera.getParameters();
                    p.setFlashMode(Parameters.FLASH_MODE_TORCH);
                    camera.setParameters(p);
                    camera.startPreview();
                    isFlashOn = true;
                    count=0;
                    Log.e("error", "App is crashed here");  
                }

            }
           return  isFlashOn;
        case KeyEvent.KEYCODE_VOLUME_DOWN:
            if (action == KeyEvent.ACTION_DOWN) {
                counter--;
                if(counter==0){

                    Parameters p = camera.getParameters();
                    p.setFlashMode(Parameters.FLASH_MODE_OFF);
                    camera.setParameters(p);
                    camera.stopPreview();
                    isFlashOn = false;
                    counter=2;
                }

            }
            return isFlashOn = false ;
        default:
           return super.dispatchKeyEvent(event);
        }
    }

When screen is off, volume buttons are only active when there's music playing, otherwise there no method to know if they are pressed. 当屏幕关闭时,只有在播放音乐时才激活音量按钮,否则无法知道是否按下了它们。 So you can play a silent song when screen is off and use whatever method you can find (onKeyDown, dispatchKeyEvent, or use broadcast receiver) but the battery will drain faster. 因此,您可以在屏幕关闭时播放无声歌曲,并使用可以找到的任何方法(onKeyDown,dispatchKeyEvent或使用广播接收器),但是电池电量消耗更快。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM