Cocos2d-x Keep the screen lit and automatically dimmed

Source: Internet
Author: User

A long time ago, the problem is now recorded. There is an Android channel (sorry, the time is too long to remember it is not clear which is the oppo/Lenovo/cool school? Rejected when we submitted a new version, the reason is: the phone backlight state, the screen does not automatically turn gray.

In order to test this, I changed it to 30 seconds, what is the effect of this? is to open any application, in 30 seconds, I do not have any action, the automatic lock screen. You can try it on your phone, such as open and then put it on, and after 30 it shows the effect is similar to pressing the Power key lock screen. But there is a difference:

1) It has a transition stage: The screen is dimmed by light, a few seconds after the screen is black, you need to press the Power key to restore;

2) No screen protection process: when the power button is pressed, the screen is lit again, showing the previous interface, such as the interface above, and directly by pressing the power button to turn off the screen, and you have set up screen protection (such as face unlock, pattern unlock, digital password unlock, hybrid password unlock, etc.), Press the power button again to light the screen is the first need to remove the screen protection;

3) No sound: After the end of the backlight countdown, the screen is black, no sound, press the power button to re-light the screen and no sound, while the power button off and lit the screen by default are sound;

The problem with the game is that no matter how long you wait, the screen is always lit up, which can cause a problem: the power will be fast because your game is always there, and the CPU and screen need to be powered by your phone's battery. To solve this problem, first look at the most important components of Android activity, its life cycle:

(Image source Baidu experience: Android activity life cycle detailed)

To solve the above problem, that is, when the activity of the Onresume () is called, the need to keep the screen is always lit, when the OnPause () is called, to release the screen is always lit, thereby transferring to the system for default processing.

About Android Screen lock, a search online there are a lot of reference, I found some also tried a lot, found that can not meet the requirements, finally found an article about flag_keep_screen_on to solve the problem, no more nonsense, directly on the code:

@Override
protectedvoidOnPause () {
//todo

Closekeepscreenon ();

Super. OnPause ();
}


@Override
protectedvoidOnresume () {
//todo

Openkeepscreenon ();

Super. Onresume ();
}


PublicBooleanBflagkeepscreen = true;
//Turn on screen always lit
PublicvoidOpenkeepscreenon ()
{
Closekeepscreenon ();

if(Bflagkeepscreen)
{
GetWindow (). SetFlags (WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, WindowManager.LayoutParams.FLAG_KEEP_ SCREEN_ON);
//GetWindow (). Addflags (WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

LOG.I ("flag_keep_screen_on","Open");
}
}

//Turn off screen always lit
PublicvoidClosekeepscreenon ()
{
if(Bflagkeepscreen)
{
GetWindow (). Clearflags (WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

LOG.I ("flag_keep_screen_on","Close");
}
}


The above commented out code, Addflags method, I test the result is useless, so instead of setflags

Resources:

Android Screen Lock-flag_keep_screen_on

Share a timed wake-up screen to unlock the keyboard and prompt the user for an instance

Keep the screen awake throughout my activity

cocos2dx resolves a lock screen issue that causes the game to run for a long time without operating on the Android platform

Cocos2d-x Keep the screen lit and automatically dimmed

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.