SoFunction
Updated on 2025-04-06

Android programming method to implement screen hibernation prohibition

This article describes the method of Android programming to prevent hibernation on screen. Share it for your reference, as follows:

There are two ways to implement this function. One is to declare it in the file, and the other is to modify the LayoutParams flag in the code. The details are as follows:

1. Use user-permission declaration in the file.The code is as follows:

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

In this way, when installing the apk, the system will prompt the installer whether to allow the hibernation function to be used.

PS: For more properties and functions, please refer to the online tools of this site:

Android Manifest function and permission description:
http://tools./table/AndroidManifest

2. Implement it in the program with code.The code is as follows:

Copy the codeThe code is as follows:
getWindow().setFlags(.FLAG_KEEP_SCREEN_ON, .FLAG_KEEP_SCREEN_ON);

Add this code to:

setContentView()

Just before.

This method will not prompt the installer to allow the use of the hibernation function during installation.

For more information about Android related content, please check out the topic of this site:Android View View Tips Summary》、《Android layout layout tips summary》、《Android development introduction and advanced tutorial》、《Android debugging skills and solutions to common problems》、《Android multimedia operation skills summary (audio, video, recording, etc.)》、《Summary of the usage of basic Android components"and"Android control usage summary

I hope this article will be helpful to everyone's Android programming design.