<span id="Label3"></p><p><p>Our program occasionally requires the screen to stay awake for a long time or longer, while the User's sleep time is set Short. This can have a certain impact on the program and the User's Use. There are two ways in Android that allow us to stay awake for long periods of time when we need to stay awake</p></p><p><p>The 1.Android power Management provides a "wake-up lock" that allows our device screen to stay awake for a certain amount of time! Here are the simple steps to Use:</p></p><p><p>Request Wakeup Lock usage rights</p></p><p><p><span class="cnblogs_code"> <span style="color: #008080;">1</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">uses-permission </span> <span style="color: #ff0000;">android:name</span><span style="color: #0000ff;">= "android.permission.WAKE_LOCK" </span> <span style="color: #0000ff;">/></span> </span></p></p><p><p>Get Power Management Objects</p></p><p><p><span class="cnblogs_code"> <span style="color: #008080;">1</span> powermanager powermanager = (powermanager)<span style="color: #0000ff;"></span>this. getsystemservice (<span style="color: #0000ff;"></span>This. power_service);</span></p></p><p><p>Get wake-up Lock Object</p></p><p><p><span class="cnblogs_code"> <span style="color: #008080;">1</span> WakeLock WakeLock = <span style="color: #0000ff;"></span>this. powermanager.newwakelock (powermanager.full_wake_lock, "My LOCK");</span></p></p><p><p>Request Wake-up Lock</p></p><pre><pre><span style="color: #008080;">1</span> <span style="color: #0000ff;">protected</span> <span style="color: #0000ff;">void</span> <span style="color: #000000;">onresume () {</span><span style="color: #008080;">2</span> <span style="color: #0000ff;">Super</span> <span style="color: #000000;">. Onresume ();</span> <span style="color: #008080;">3</span> <span style="color: #008000;">//</span> <span style="color: #008000;">is awake until the wake -up lock with release</span> <span style="color: #008080;">4</span><span style="color: #000000;"> wakelock.acquire ()</span> is used; <span style="color: #008080;">5</span> <span style="color: #008000;">//</span> <span style="color: #008000;">waking a given time</span> <span style="color: #008080;">6</span> Wakelock.acquire (<span style="color: #0000ff;">long</span> <span style="color: #000000;"> timeout);</span> <span style="color: #008080;">7</span> }</pre></pre><p><p>Release wake-up Lock</p></p><pre><pre><span style="color: #008080;">1</span> <span style="color: #0000ff;">protected</span> <span style="color: #0000ff;">void</span> <span style="color: #000000;">onPause () { </span><span style="color: #008080;">2</span> <span style="color: #0000ff;">Super</span><span style="color: #000000;">. OnPause (); </span> <span style="color: #008080;">3</span> <span style="color: #000000;"> wakelock.release (); </span> <span style="color: #008080;">4</span> } </pre></pre><p><p>If acquire is used, the screen will remain awake until the user exits the program or manually presses the Power key to turn off the Screen.</p></p><p><p>2. How to stay awake using the screen provided by the window Manager</p></p><pre><pre><span style="color: #008080;">1</span> <span style="color: #0000ff;">protected</span> <span style="color: #0000ff;">void</span> <span style="color: #000000;">onCreate (Bundle Icicle) {</span><span style="color: #008080;">2</span> <span style="color: #0000ff;">Super</span> <span style="color: #000000;">. OnCreate (icicle); </span> <span style="color: #008080;">3</span> <span style="color: #008000;">//</span> <span style="color: #008000;">set flag to make this activity wake up</span> <span style="color: #008080;">4</span> <span style="color: #000000;"> getWindow (). addflags (WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);</span> <span style="color: #008080;">5</span> <span style="color: #000000;"> Setcontentview ();</span> <span style="color: #008080;">6</span> </pre></pre><p><p>The advantage of this way is not to worry about the release of the problem, the page exited, automatically lifted the wake state, into the normal mode</p></p><p><p></p></p><p><p>Android screen remains awake status</p></p></span>
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service