Android powermanager and powermanager. wakelock and WiFi lock

Source: Internet
Author: User

Powermanager = (powermanager) getsystemservice (context. power_service );
Mwakelock = powermanager. newwakelock (powermanager. partial_wake_lock,
"AAAAA ");
Mwakelock. setreferencecounted (true );
// Create a WiFi lock
Wifimanager = (wifimanager) getsystemservice (context. wifi_service );
Mwifilock = wifimanager. createwifilock ("cbsradioplayer ");
Mwifilock. setreferencecounted (true );
Mwakelock. Acquire ();
// Acquire WiFi lock
Mwifilock. Acquire ();

 

 

The screen brightness is maintained and WiFi is not disabled. If the screen is not operated for a long time, the phone will enter sleep and WiFi will be disabled. If the streaming media playback service is available

Using your 3G traffic would be a huge expense!

Powermanager. wakelock powermanager. wakerlock is a small point I found when analyzing the standup timer source code, standup
Timer uses wakelock to ensure that the screen on the mobile phone is always bright while running the program (the program is small but also very careful and considerate ). Powermanager
And powermanager. wakerlock7 are used to manage the power supply of Android devices. Powermanager
: This class gives you control of the power state of the device. powermanager. wakelock
: Lets you say that you need to have the device on. Android controls the power supply through various lock locks. Note that the lock and unlock must appear in pairs. First, the code in the previous standup timer is described. Code

private void acquireWakeLock() {
if (wakeLock == null) {
Logger.d("Acquiring wake lock");
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
wakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, this.getClass().getCanonicalName());
wakeLock.acquire();
}

}


private void releaseWakeLock() {
if (wakeLock != null && wakeLock.isHeld()) {
wakeLock.release();
wakeLock = null;
}

}

 

The screen_dim_wake_lock lock is obtained in the acquirewakelock () method, which causes the CPU
Keep running, and the screen brightness (can be dimmed ). This function is in the activity
Onresume is called. The releasewakelock () method is to release the lock. It is in the activity's
Called in onpause. The Activiy life cycle is used to skillfully present acquire () and release () in pairs.

@ Override
Protected void onresume ()
{
Super. onresume ();
// Obtain the lock to keep the screen brightness
Acquirewakelock ();
Starttimer ();
}
Code

 

Procedure for powermanager and wakelock
  1. Powermanager PM = (powermanager) getsystemservice (context. power_service );Context.getSystemService()

    Method to obtain the powermanager instance.
  2. Then, use powermanager's newwakelock
    (INT flags, string
    To generate a wakelock instance. Int flags indicates the type of wakelock to be obtained. Different locks have different effects on CPU, screen, and keyboard lights.
  3. After obtaining the wakelock instance, obtain the corresponding lock through acquire (), perform other business logic operations, and finally use release () to release (release is required ).

About int flags
The impact of various lock types on the CPU, screen, and keyboard:

Partial_wake_lock

: Keep the CPU running. The screen and keyboard lights may be disabled.

Screen_dim_wake_lock

: Keep the CPU running. It allows screen display but may be gray. You can turn off the keyboard light.

Screen_bright_wake_lock

: Keep the CPU running, enable screen highlighting, and disable keyboard lights

Full_wake_lock

: Keep the CPU running, screen highlighted, and keyboard light brightness

Acquire_causes_wakeup

: Normal
Wake locks don't actually turn on the illumination. Instead, they cause
The illumination to remain on once it turns on (e.g. From user
Activity). This flag will force the screen and/or keyboard to turn on
Immediately, when the wakelock is acquired. A typical use wocould be
Restrictions which are important for the user to see immediately.

On_after_release

: F
This flag is set, the user activity timer will be reset when
Wakelock is released, causing the illumination to remain on a bit
Longer. This can be used to reduce flicker if you are using ing
Wake lock conditions.

You must declare in androidmanifest. XML that the application has the permission to set power management.
<Uses-Permission Android: Name = "android. Permission. wake_lock"/>
You may need
<Uses-Permission Android: Name = "android. Permission. device_power"/>

In addition, the settings of wakelock are Activiy-level, not for the entire application.

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.