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.