Solid-screen control

Source: Internet
Author: User




1. Write an Android program, in the program can choose whether to keep the screen solid (set to "yes" is solid, "no" to follow the system), the program exit or minimize will not keep the screen always bright, the more concise the code the better, thank you
onCreate(){      getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);  } onStop(){      getWindow().removeFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); }2. Advanced, lock screen type
Partial_wake_lock: Keep the CPU running, and the screen and keyboard lights may be off.
Screen_dim_wake_lock: Keep the CPU running, allow the screen to be displayed but may be gray, allow to turn off the keyboard light
Screen_bright_wake_lock: Keeps the CPU running, allowing the screen to be highlighted, allowing the keyboard light to be turned off
Full_wake_lock: Keep the CPU running, keep the screen highlighted, and the keyboard lights remain bright

I. Preface
Many of our Android applications need to work with the screen and the screen, such as the alarm clock, need to keep the screen, and then need to extinguish the screen. So today, let's analyze the features in this area.

Two. System Service---Powermanager.java
As the name implies, Powermanager.java is the management of our power functions, of course, including our screen light and off. Yes, our application is to use this system service to achieve the function of light screen and extinguish screen.
1. The application obtains the PowerManager service, we can write like this:
PowerManager pm = (powermanager) getsystemservice (Context.power_service);
Because PowerManager is a system service, its life cycle is not controlled by the application, the application can only request the PowerManager service through the system, and then the system will apply this service to us.
2. Gotosleep () method, this method can force our screen to extinguish the screen. We can call it this way:
After getting the PM object above, this invokes Pm.gotosleep (Systemclock.uptimemillis ());
3. Setbacklightbrightness () method, this method can set the brightness of the backlight, from 0-255

Three. Timer off screen
So here, you can also introduce a control screen method is Powermanager.wakelock
As the name implies, Wakelock this thing is the light screen control, here said the bright screen, it defines several types of bright screen. As follows:
Type Cpu Screen Keyboard
Partial_wake_lock On Off Off
Screen_dim_wake_lock On Dim Off
Screen_bright_wake_lock On Bright Off
Full_wake_lock On Bright Bright

Since it defines so many types, each type can specify that the corresponding part works and does not work. Then we can take advantage of these types, from the finest granularity to control our screen, control our power, so that our power supply working time as long as possible (we all know that the smartphone battery problem is a mishap, a daily charge, there is wood there?) Others say that the man who uses the Android phone is a good family man, because he has to go home to recharge every night. ^^).
All right, all right, gossip doesn't work.
How to use this wakelock? We can write this:
PowerManager pm = (powermanager) getsystemservice (Context.power_service);
Powermanager.wakelock WakeLock = Pm.newwakelock (Powermanager.screen_dim_wake_lock, "TAG");
Wakelock.acquire ();
Do our job, at this stage, our screen will continue to light up
Release the lock and the screen goes out.
Wl.release ();

So here we can also use, that is, how much time after the screen off
First, light the screen.
PowerManager pm = (powermanager) getsystemservice (Context.power_service);
Powermanager.wakelock WakeLock = Pm.newwakelock (Powermanager.screen_dim_wake_lock, "TAG");
Wakelock.acquire ();
And then
Mtimehandler.postdelayed (New Runnable () {
public void Run () {
Wakelock.release ();
}
}, 10*1000);
Well, after the delay of 10s off the screen ....
So, it's important to note that acquire () and release () are called in pairs. In other words, you apply for a light screen, and over time, if you release it.

A little bit of analysis, only to be a point ~


Control with a solid screen

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.