Android Internship notes (2)---magical view keepscreenon keep your screen solid

Source: Internet
Author: User

Android Internship notes (2)---magical view keepscreenon keep your screen solid

--Reprint Please specify Source: Coder-pig


1) What is the screen solid?

The literal translation is to let our screen is always on , the phone is usually equipped with power-saving mode or something, such as set no operation, 15 seconds

The screen turns off automatically, and if it happens that our program is doing something, a sudden screen shutdown may cause some problems;

For example, the app is being installed or the app is updating, downloading, processing data, and so on, we need to make the phone screen at a particular time

Keep the light on, so how do you keep the screen solid?



2) How to keep the screen solid?


Two ways to mainstream:


① is implemented through Powermanager.wakelock:

Step 1: Get the System PowerManager object, then create the Wakelock instance through the Newwakelock method

Step 2: In order to not affect other apps, we usually put the keep-solid method in the Onresume () method instead of the solid

Method into the OnPause () method

Step 3: You will also need to add permissions in the Androidmanifest.xml file, as well as prompts when installing, whether to allow

Use the Disable hibernation feature! Permissions are as follows:

<uses-permission android:name= "Android.permission.WAKE_LOCK"/>

The simple use code is as follows:

Get the System Power_service object PowerManager PM = (powermanager) getsystemservice (Context.power_service); Create an WakeLock instance from the Newwakelock () method  Powermanager.wakelock wl = Pm.newwakelock (Powermanager.screen_dim_wake_lock, " Here you write, a tag ");  It is best to put the Onreusme method call Wl.acquire ();//preferably put to OnPause method call    

for the first parameter of Newwakelock, the following options are available:

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





② directly through GetWindow (). Addflags implementation

This is relatively simple, which activity to use, just add the following code in the OnCreate () method:


and do not need to add permissions, many online are pits, only the setting is often bright, how not to say, more and more found Golgoby Niang use!

addflags () to add tags, then, to cancel the normal light just need us to clear the mark, so we just need to change the addflags to

clearflags () ! The reference code is as follows:


Package Com.jay.example.keepscreenondemo;import Android.os.bundle;import Android.app.activity;import Android.view.view;import Android.view.view.onclicklistener;import Android.view.windowmanager;import Android.widget.button;public class Mainactivity extends Activity {private Button btnclose;    @Override    protected void onCreate (Bundle savedinstancestate) {        super.oncreate (savedinstancestate);        Setcontentview (r.layout.activity_main);        Btnclose = (Button) Findviewbyid (r.id.btnclose);        GetWindow (). Addflags (WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);         When the button is clicked, the mark is cleared and the solid screen effect disappears        Btnclose.setonclicklistener (new Onclicklistener () {@Overridepublic void OnClick (View View) {GetWindow (). Clearflags (WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);}});}    }


3) The Keepscreenon implementation of the view component is always illuminated:

In front of the wordy introduction of two ways to keep the screen solid, as if Tricky ha, this sectionto is Keepscreenon attribute;

But there is nothing to learn more than 2 ways ha! using the Keepscreenon property is simple

Step 1: In the layout file for the Activity that you need to always light, give a UI component a set:android:keepscreenon= "true"

Then, as long as the activity is not stopped, then the screen will remain solid!

Step 2: How do I turn it off? It's pretty simple. Modify the properties of the UI component when you need to turn it off, and set it to false .

Compared to the previous two, this is not more convenient?


Also put down the simple code of Use:

Main.xml:

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"    xmlns:tools= "http// Schemas.android.com/tools "    android:id=" @+id/linearlayout1 "    android:layout_width=" Match_parent    " android:layout_height= "Match_parent"    android:orientation= "vertical"    tools:context= ". Mainactivity ">    <button        android:id=" @+id/btnclose "        android:layout_width=" Wrap_content        " android:layout_height= "Wrap_content"        android:text= "off"/>    <textview        android:id= "@+id/ Txtliang "        android:layout_width=" wrap_content "        android:layout_height=" Wrap_content "        android: Keepscreenon= "true"        android:text= "I am so Bright"/></linearlayout>

Mainactvitiy.java

Package Com.jay.example.keepscreenondemo;import Android.os.bundle;import Android.app.activity;import Android.view.view;import Android.view.view.onclicklistener;import Android.view.windowmanager;import Android.widget.button;import Android.widget.textview;public class Mainactivity extends Activity {private Button    btnclose;private TextView txtliang;private int flag =-1;        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main);        Btnclose = (Button) Findviewbyid (r.id.btnclose);                Txtliang = (TextView) Findviewbyid (R.id.txtliang); Btnclose.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {if (flag = =-1) { Txtliang.setkeepscreenon (false); Txtliang.settext ("I can't light Up =-="); Btnclose.settext ("Bright");}     else {Txtliang.setkeepscreenon (true); Txtliang.settext ("I'm light-^"); Btnclose.settext ("not Bright");}}); }}


4) Finally say a few words:

① solid premise is that you do not press the Power key ha ~ ~ ~ a press affirmation black screen, (*^__^*) ...

② code is relatively simple, it is not affixed, the other top of the screen is mostly solid, do not post

Their mobile phone to adjust the power-saving mode, the screen timed out to 15s, and then drying one side, and then you know the test!






Android Internship notes (2)---magical view keepscreenon keep your 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.