保持清醒的屏幕在我的活动 [英] Keep the screen awake throughout my activity

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

问题描述

我在我的应用程序三项活动。我想保持屏幕清醒的时候是在第二个活动。屏幕不应该在我的第二个活动熄灭,除非锁键手动pssed $ P $。我经历了很多联系,但他们似乎我不清楚。

I have three activities in my app. I want to keep the screen awake when it is in the second activity. The screen should not go off in my second activity unless the "lock" key is pressed manually. I went through many links but they seem unclear to me.

推荐答案

如上所述,在Android教程保持屏幕在,你可以在几个方面做到这一点。您可以在活动的窗口中设置FLAG_KEEP_SCREEN_ON:

As discussed in the Android tutorial Keep the Screen On, you can do this in a few ways. You can set the FLAG_KEEP_SCREEN_ON on the activity's window:

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

这是XML等价物即是属性机器人添加:keepScreenOn =真正的来您的活动布局的根视图。编程设置标志的好处是,你可以使用

An XML equivalent for that is to add the attribute android:keepScreenOn="true" to the root view of your activity's layout. The advantage of setting the flag programmatically is that you can use

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

当你不再需要强制在屏幕上停留,而你的活动正在运行。

when you no longer need to force the screen to stay on while your activity is running.

的另一种方法,以控制在屏幕上(和某些其它资源)是使用一个唤醒锁:

Another way to control the screen (and certain other resources) is to use a wake lock:

mWakeLock = getContext().getSystemService(Context.POWER_SERVICE)
    .newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK, getClass().getName());
mWakeLock.acquire();
// screen stays on in this section
mWakeLock.release();

该清单必须包括此权限:

The manifest will have to include this permission:

<uses-permission android:name="android.permission.WAKE_LOCK"/>

然而,如在本教程所讨论的,一个唤醒锁是更适合于其他的用例(例如服务或后台任务需要在CPU保持运行而屏幕是关闭的)。

However, as discussed in the tutorial, a wake lock is more appropriate for other use cases (such as a service or background task needing the CPU to keep running while the screen is off).

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

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