setKeepScreenOn / FLAG_KEEP_SCREEN_ON 的正确方法
我正在使用 setKeepScreenOn(true) 方法,但无法弄清楚如何相对于当前活动(具有内容视图集)来调用此方法。我已经能够通过在视图中始终存在的一个按钮上调用它来使其工作,但这感觉不对 - 而且我确信一定有一种方法可以解决这个问题。我尝试像这样引用当前焦点:
getCurrentFocus().setKeepScreenOn(true);
但这引发了 NullPointerException。也许当前没有焦点。 那么,谁能告诉我如何引用我正在内部工作的视图类?谢谢 :)
I am using the method setKeepScreenOn(true) and haven't been able to figure out how to call this in relation to the current Activity (which has a content view set). I've been able to get it to work by calling it on one of my buttons which is always present in the view, but this feels wrong - and I'm sure there must be a way to get around this. I tried referencing the current focus like this:
getCurrentFocus().setKeepScreenOn(true);
but that threw a NullPointerException. Maybe there was no current focus.
So, can anyone tell me how I can reference the view class which I am working inside? Thanks :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
尝试这个答案:
getWindow
是为 Activity 定义的方法,不需要您首先找到View
。Try this answer:
getWindow
is a method defined for activities, and won't require you to find aView
first.正如霍克所说,但解释得很差。
您还可以在 XML 布局文件中使用
FLAG_KEEP_SCREEN_ON
。请注意
android:keepScreenOn="true"
我现在已将保持屏幕打开的所有选择写入博客文章中:
https://blog.blundellapps.co.uk /tut-keep-screen-onawake-3-possible-ways/
As Hawk said but poorly explained.
You can also use
FLAG_KEEP_SCREEN_ON
in your XML layout file.Note the
android:keepScreenOn="true"
I've now written all the choices for keeping the screen on up into a blog post:
https://blog.blundellapps.co.uk/tut-keep-screen-onawake-3-possible-ways/
在 XML 中设置
android:keepScreenOn
Set
android:keepScreenOn
in XML如果您在类扩展视图上执行此操作。你可以简单地:
If you are doing it on a class extends View. You can simple:
根据 android 开发者的 Google 文档,您有两种方法可以做到这一点:
首先way :
第二种方法是在 xml 文件布局中添加此属性:
android:keepScreenOn="true"
According to Google Docs for android Developers you've two ways to do this :
First way :
Second way is to add in your xml file layout this attribute:
android:keepScreenOn="true"