Reputation: 3221
I'm having problem allow the device to dim and turn off after using WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
.
I've built a recording camera and while recording I don't want the screen to turn off so I used the WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
and its work fine.
The problem is that after recording (while still in the same activity) I wand to set the device to the previous state (after couple of seconds dim, after couple of seconds more then it completely off).
I follow this android code:
https://developer.android.com/training/scheduling/wakelock.html
and its says to use getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
, but nothing happens and the screen is kept on.
Anyone knows how to let the screen turn off?
Here is my code:
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
}
};
Upvotes: 0
Views: 2297
Reputation: 414
You Can use this tutorial to dim the light. Some snippets are :(brightness is an integer value)
IHardwareService hardware = IHardwareService.Stub.asInterface(
ServiceManager.getService("hardware"));
if (hardware != null) {
hardware.setScreenBacklight(brightness);
}
Another Way is :
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);
Upvotes: 0
Reputation: 22193
You could play with some View using in your xml the attribute keepScreenOn. In this way when the view is no more visible the flag is no more valid automatically. doc
Upvotes: 0