setKeepScreenOn / FLAG_KEEP_SCREEN_ON 的正确方法

发布于 2024-10-22 19:52:48 字数 284 浏览 2 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

沦落红尘 2024-10-29 19:52:48

尝试这个答案

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

getWindow 是为 Activity 定义的方法,不需要您首先找到 View

Try this answer:

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

getWindow is a method defined for activities, and won't require you to find a View first.

油饼 2024-10-29 19:52:48

正如霍克所说,但解释得很差。

您还可以在 XML 布局文件中使用 FLAG_KEEP_SCREEN_ON

请注意 android:keepScreenOn="true"

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:keepScreenOn="true"
    android:orientation="vertical" >

    <!-- whatever is in your layout -->

</LinearLayout>

我现在已将保持屏幕打开的所有选择写入博客文章中:
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"

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:keepScreenOn="true"
    android:orientation="vertical" >

    <!-- whatever is in your layout -->

</LinearLayout>

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/

划一舟意中人 2024-10-29 19:52:48

在 XML 中设置 android:keepScreenOn

Set android:keepScreenOn in XML

咋地 2024-10-29 19:52:48

如果您在类扩展视图上执行此操作。你可以简单地:

this.setKeepScreenOn(true);

If you are doing it on a class extends View. You can simple:

this.setKeepScreenOn(true);
孤城病女 2024-10-29 19:52:48

根据 android 开发者的 Google 文档,您有两种方法可以做到这一点:

首先way :

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}

第二种方法是在 xml 文件布局中添加此属性:
android:keepScreenOn="true"

According to Google Docs for android Developers you've two ways to do this :

First way :

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}

Second way is to add in your xml file layout this attribute:
android:keepScreenOn="true"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文