保持屏幕上的活动 - 不与FLAG_KEEP_SCREEN_ON工作 [英] Keep screen on in Activity - does not work with FLAG_KEEP_SCREEN_ON

查看:543
本文介绍了保持屏幕上的活动 - 不与FLAG_KEEP_SCREEN_ON工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,很长一段时间,我想我知道如何进入睡眠模式停止屏幕,我只是用这个code在我的活动:

So, for a long time I thought that I knew how to stop the screen from going into sleep mode, I simply used this code in my Activity:

getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

不过,我意识到,这只是工作时,我的手机在开发者模式,即当USB调试(设置 - > USB调试 - >开发者选项)检查已启用/。接着上面的codeS确实停止屏幕/设备去睡觉。

However, I realized that this only worked when my phone was in "developer mode", ie when the usb debugging (Settings --> Developer options --> USB debugging) was enabled/checked. Then the above codes indeed stops the screen/device to go to sleep.

在该调试没有被选中,那么我的屏幕进入睡眠状态就像没有明天。我为我的设备上运行Android 4.04和

When that debugging is not checked, then my screen goes to sleep like there's no tomorrow. I am running Android 4.04 on my device, and

 android:minSdkVersion="12"
 android:targetSdkVersion="16"

没有人听说过这个问题?

Anyone heard about this issue?

修改

我已经测试与Commonswares建议,并添加setKeepScreenOn()到code,所以它看起来是这样的:

I have tested with Commonswares suggestion, and added the setKeepScreenOn() to the code, so it looks like this:

getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); 
View root = findViewById(android.R.id.content); 
if (root != null)
    root.setKeepScreenOn(true);

我还检查,以便实际执行该code,它是。但它不改变任何事情...

I have also checked so that this code is actually executed, and it is. But it doesnt change a thing...

推荐答案

我面临同样的问题,我用一个活动我的项目,所有的片段,然后我使用了 Android的其它屏幕: keepScreenOn =真正的的主要活动。

i was facing same problem, i was using one activity for my project and all the other screen are fragments then i used the android:keepScreenOn="true" in main activity.

请尝试使用此,让我知道,如果你没有得到你的愿望的结果。

please try to use this and let me know if you didn't get your desire result.

感谢。

这篇关于保持屏幕上的活动 - 不与FLAG_KEEP_SCREEN_ON工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆