Developer on Alibaba Coud: Build your first app with APIs, SDKs, and tutorials on the Alibaba Cloud. Read more ＞
The power management of Android is also an important part of it. For example, when you turn off unused equipment on standby, after the timeout screen and keyboard backlight closed, users to operate the number of devices to open and so on, these are directly related to the product standby time, as well as the user experience. On the Internet can find a reprinted around the Power Management article http://blog.csdn.net/hzdysymbol/article/details/4004791, but also from top to bottom, More attention to the bottom how to apply. This article is only about the analysis of the framework layer and how to tailor it to the requirements. The framework layer mainly has these two files: Frameworks\base\core\java\android\os\powermanager.java frameworks\base\services\java\com\ Android\server \powermanagerservice.java where Powermanager.java is provided to the application layer, the final core is the Powermanagerservice.java. The function of this class is to provide PowerManager functionality and the operation of the entire power management state machine. Inside the function and analogy is more, from the external and internal divided into two pieces. First say external, powermanagerservice how to carry on power management, that must have outside event time to inform it, this is mainly in Frameworks\base \services\java\com\android\server\ Windowmanagerservice.java inside. Windowmanagerservice will call the user's click Screen, keystrokes, and so on as an activity event to invoke the Useractivity function, Powermanagerservice will be in the useractivity to judge the event type to reflect, is to light the screen to provide the operation, or completely ignore, or only on the turn off. The methods for Windowmanagerservice calls are gotosleep and other functions that get the power state, such as Screenison, and so on. At the internal, as the external interface of the Useractivity method is mainly through the setpowerstate to complete the function. To set the power status such as the switch screen backlight and so on as a parameter call Setpowerstate,setpowerstate first judge the desired state can be completed, such as the point of light screen, butNow that the screen is locked, it can't be lit, otherwise you can call Power.setscreenstate (true) to run through JNI into the driver to light up the screen. And the state cycle of the power supply is mainly realized through handler. Powermanagerservice in Init will start a handlerthread a background message loop to provide delayed delivery of tasks, you can use handler to delay a task's execution time, thus implementing the state machine loop. For example, timeout, after a period of no action to make the screen darkened, and then closed, reflected in the code as follows: Useractivity inside after the call setpowerstate will use settimeoutlocked to set timeout. Then, in Settimeoutlocked, the next state and time are computed according to the current state, and then the Mhandler.postattime (Mtimeouttask, when) is called to post a timeouttask. This will perform timeouttask after the MS. In Timeouttask, the setpowerstate is invoked according to the state of the set to change the power state, and then set a new state, for example, now the screen is dimmed from the light, then use the settimeoutlocked. Let's wait and turn the screen off completely. If this time the screen is turned off, the timeout state cycle of the round is over. If you want to customize it, such as the need to turn off some peripherals after the timeout screen is switched off, and so on, it's better to turn off the screen in the Timeouttask and then close the other device's code. Even if the new state demand is completely different from the original, it should be easy to use handler. The logic is clear to put the code in the right place.
This article is an English version of an article which is originally in the Chinese language on aliyun.com and is provided for information purposes only. This website makes no representation or warranty of any kind, either expressed or implied, as to the accuracy, completeness ownership or
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:
and provide relevant evidence. A staff member will contact you within 5 working days.