How to prevent the screen of an android device to

2020-02-08 10:09发布

I have an Activity that usually needs some time to watch the screen without interacting with it.

The problem is that the screen turns off, just like with almos any other app. I want to prevent that automatic turn off for my Activity, like Dolphin HD does (if you configure it from the settings menu).

I want to let the user to turn off the screen pressing the usual block button, but prevent the automatic block of the device while in my Activity.

How can I achieve this?

Thanx.

6条回答
Explosion°爆炸
2楼-- · 2020-02-08 10:31

You may want to use the wake-lock to prevent the screen off.Pleas refer http://developer.android.com/reference/android/os/PowerManager.WakeLock.html

查看更多
乱世女痞
3楼-- · 2020-02-08 10:32

Add android:keepScreenOn="true" to some widget in your layout XML resource for this activity. So long as that widget is visible on the screen, the screen will not turn off automatically.

EDIT:

A WakeLock, as suggested by other answers, technically will work. But then you have to manually release the WakeLock (if you mess that up, the screen will stay on a long time). And, since you could mess it up, you need the WAKE_LOCK permission. Using keepScreenOn avoids all of that.

查看更多
贼婆χ
4楼-- · 2020-02-08 10:38

Handling Fragments and Screen Rotation

The Android documentation, Keep the device awake outlines each solution.

Solution #1 - Flags

Documentation - Alternatives to using wake locks

For Fragments use the programmatic approach that is less battery intensive.

Enable

activity!!.window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)

Disable

activity!!.window.clearFlags(LayoutParams.FLAG_KEEP_SCREEN_ON)

For my use case I called this in onStop() so the screen would default to the normal configuration when the Fragment showing the media content is exited.

Avoid

keepScreenOn=true does not work when there is a configuration change such as a screen rotation for Fragments.

Note: Android's Keep the device awake documentation should be updated accordingly to handle this case.

Solution #2 - Wake Lock

Documentation - Keep the CPU on with Wake Locks

A Wake Lock offers more control over keeping the specific elements of the device awake, but is more battery intensive, and important to release manually to save the battery since it is not handled automatically by the system.

查看更多
淡お忘
5楼-- · 2020-02-08 10:43

To change it on-the-fly do this:

if (keepScreenOn)
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
else
    getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
查看更多
家丑人穷心不美
6楼-- · 2020-02-08 10:52

You'll want to add WAKE_LOCK to your manifest, and set and remove it as needed within your app. See the google docs here for PowerManager.WAKE_LOCK

http://developer.android.com/reference/android/os/PowerManager.html

查看更多
冷血范
7楼-- · 2020-02-08 10:57

For "Xamarin Android":

Window.AddFlags(WindowManagerFlags.KeepScreenOn);
查看更多
登录 后发表回答