Android WakeLock usage code example

Source: Internet
Author: User

Android provides a class named WakeLock in android. OS. powerManager. in WakeLock, from the perspective of name, WakeLock is the meaning of the wake-up lock, which can control the backlight switch of the screen, so it is in the power management class.
The WakeLock instantiation method is relatively simple, because it is a remote service of the system, which is constructed through the following code. Copy codeThe Code is as follows: PowerManager pm = (PowerManager) getSystemService (Context. POWER_SERVICE );
PowerManager. WakeLock wl = pm. newWakeLock (PowerManager. SCREEN_DIM_WAKE_LOCK, "Android123"); // The last parameter is the Instance name, which can be changed to another one.
Wl. acquire (); // wake up to light up the screen
// The screen will light up during this period
Wl. release (); // restore the screen to the dark

Of course, Android does not mean that developers can control the display or absence of backlights on the screen without permission. Only the backlights lit by acquire can use release to turn off the backlights, if you directly call the release method to close the screen, an exception will occur.
The Android 2.1 API Level7 adds a public boolean isScreenOn () method to determine whether the screen is lit. The code isCopy codeThe Code is as follows: PowerManager pm = (PowerManager) getSystemService (Context. POWER_SERVICE );
Boolean isScreenOn = pm. isScreenOn ();

When the Android 2.2 API Level is 8, a reboot () is added. You can restart your mobile phone to enter the recovery mode, and you need to apply for permissions to achieve this, however, Android development network does not guarantee that this method works on all firmware instances. Some vendors have not developed this Restart Method for security considerations.

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.