How Android keeps the screen solid

Source: Internet
Author: User

The Newwakelock method has been used before:

         This  This                 . Getsystemservice (Context.power_service);          This  This . Powermanager.newwakelock (                "My Lock"); // Keep Solid         This . Wakelock.acquire (); // Release        this. wakelock.release ();

Recently found flag_keep_screen_on more convenient

 Public class extends activity{@Overrideprotectedvoid onCreate (Bundle savedinstancestate) {super. OnCreate ( Savedinstancestate); Setcontentview (R.layout.activity_main); GetWindow (). Addflags ( WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);}}

The advantage of this approach is that, unlike wakelocks, it does not require specific permissions, and in the case of different application operations, the system is managed without worrying about unused resources that are not released.

You do not need to clear the FLAG_KEEP_SCREEN_ON flag unless you no longer need to stay on the screen when you run the application. When the application enters the background or returns to the foreground, the window manager is responsible for ensuring normal event handling, but if you explicitly want to clear the Peugeot and allow the screen to go out, you can use Clearflags ().

As follows:

GetWindow (). Clearflags (WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)

Another way: In an XML file, use the Android:keepscreenon property

< Relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "Match_parent" Android: layout_height= "Match_parent" android:keepscreenon= "true" >  ... </ Relativelayout >

This setting android:keepscreenon= "true" is equivalent to flag_keep_screen_on.

How Android keeps the screen solid

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.