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

问题描述 投票:0回答:2
我在使用WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON后让设备变暗并关闭电源时遇到问题。

我已经建立了一个摄录机,并且在录制时我不希望屏幕关闭,所以我使用了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 } };

我在使用WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON后允许设备变暗并关闭电源时遇到问题。我已经建立了一个摄录机,并且在摄录时我不希望屏幕转动...
android android-windowmanager android-wake-lock
2个回答
0
投票
您可以使用此tutorial使光线变暗。一些片段是:(亮度是一个整数值)

0
投票
您可以在XML中使用属性keepScreenOn来玩一些View。这样,当视图不再可见时,标记将不再自动有效。 doc
© www.soinside.com 2019 - 2024. All rights reserved.