使用“WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON”后屏幕关闭

时间:2014-12-31 09:03:00

标签: android android-windowmanager android-wake-lock

I m having problem allow the device to dim and turn off after using WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON`。

Iv e built recording camera and while recording I dont want the screen to turn off so Id used the WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON`及其工作正常。 问题是,在录制之后(同时仍处于同一活动中),我将魔法设备设置为之前的状态(几秒钟之后昏暗,几秒钟之后再完全关闭)。 我按照这个Android代码:

https://developer.android.com/training/scheduling/wakelock.html

并且它说使用getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON),但没有任何反应,屏幕保持开启状态。

有人知道让屏幕关闭吗?

这是我的代码:

 private View.OnClickListener startRecord=new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        if (isRecording) {
            // stop recording and release camera
            mMediaRecorder.stop();  // stop the recording
            releaseMediaRecorder(); // release the MediaRecorder object
            mCamera.lock();         // take camera access back from MediaRecorder

            // inform the user that recording has stopped

            isRecording = false;
            videoV.setAlpha(1f);
            videoV.setClickable(true);
            videoX.setAlpha(1f);
            videoX.setClickable(true);
            toggleRecord.setAlpha(0f);
            toggleRecord.setClickable(false);
            timerCountDown.cancel();//chancel timer counter
            animationCountDown.cancel();//chancel animation counter
            videoCountDown.setText("");
            videoCounterFrame.setBackgroundColor(Color.parseColor("#00000000"));  // set counter alpha to 0
            getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);  // after recording, allow screen turn off
        } else {
            // initialize video camera
            if (prepareVideoRecorder()) {
                // Camera is available and unlocked, MediaRecorder is prepared,
                // now you can start recording
                toggleRecord.setImageResource(R.drawable.vidcam_stop);
                mMediaRecorder.start();

                // inform the user that recording has started

                isRecording = true;
                videoCounterFrame.setBackgroundColor(Color.parseColor("#30000000"));//set alpha to 0.3
                //init two animations- first change animation from white to red and second the opposite
                final ValueAnimator colorAnimation = ValueAnimator.ofObject(new ArgbEvaluator(), Color.parseColor("#ffffff"), Color.parseColor("#f50f2b"));
                colorAnimation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

                    @Override
                    public void onAnimationUpdate(ValueAnimator animator) {
                        videoCountDown.setTextColor((Integer)animator.getAnimatedValue());
                    }

                });

                final ValueAnimator colorAnimation2 = ValueAnimator.ofObject(new ArgbEvaluator(), Color.parseColor("#f50f2b"), Color.parseColor("#ffffff"));
                colorAnimation2.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

                    @Override
                    public void onAnimationUpdate(ValueAnimator animator) {
                        videoCountDown.setTextColor((Integer)animator.getAnimatedValue());
                    }

                });
                //start both counters
                timerCountDown=new CountDownTimer(121000, 100) {

                    public void onTick(long millisUntilFinished) {
                        Log.i("millis",millisUntilFinished+"");
                        if(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished)>0){
                            videoCountDown.setText(String.format("%d:%02d",
                                    TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished),
                                    TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) -
                                            TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished))));
                        }else{

                            videoCountDown.setText((millisUntilFinished-1000)/1000+"");
                        }

                    }

                    public void onFinish() {
                        videoCountDown.setText("0");
                        toggleRecord.performClick();
                    }
                }.start();

                animationCountDown=new CountDownTimer(121000, 600) {

                    public void onTick(long millisUntilFinished) {
                        if(animationColorChange){
                            colorAnimation.start();
                            animationColorChange=false;
                        }else{
                            colorAnimation2.start();
                            animationColorChange=true;
                        }

                    }

                    @Override
                    public void onFinish() {
                        animationColorChange=true;
                    }

                }.start();

            } else {
                // prepare didn't work, release the camera
                releaseMediaRecorder();
                // inform user
            }
        }
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); // screen never goes to sleep while recording
    }


};

2 个答案:

答案 0 :(得分:0)

您可以使用此tutorial来调暗光线。 一些片段是:(亮度是整数值)

  IHardwareService hardware = IHardwareService.Stub.asInterface(
  ServiceManager.getService("hardware"));
  if (hardware != null) {
    hardware.setScreenBacklight(brightness);
  }

另一种方式是:

Settings.System.putInt(this.getContentResolver(),
    Settings.System.SCREEN_BRIGHTNESS, 20);

   WindowManager.LayoutParams lp = getWindow().getAttributes();
  lp.screenBrightness =0.2f;// 100 / 100.0f;
  getWindow().setAttributes(lp);

答案 1 :(得分:0)

你可以在你的xml中使用一些View使用属性keepScreenOn。这样,当视图不再可见时,标志不再自动有效。 doc