Android Practice Notes (2) --- use keepScreenOn of View to keep the screen always on and android screen always on
Android Practice Notes (2) --- use keepScreenOn of View to keep the screen always on
-- Reprinted with the source: coder-pig
1) What is the screen always on?
Our screen is always on. Cell phones usually have power-saving modes, for example, 15 seconds after no operation is set.
The screen is automatically closed. If our program is performing some operations, suddenly closing the screen may cause some problems;
For example, the app is being installed or the app is updating, downloading, and processing data. Therefore, we need to make the mobile phone screen
How can we keep the screen bright?
2) How to Make the screen always bright?
Two mainstream methods:
① Implemented through PowerManager. WakeLock:
Step 1: Obtain the system PowerManager object, and then use the newWakeLock method to create a WakeLock instance.
Step 2: in order not to affect other apps, we usually remove the always-on method from the onResume () method.
Put the method in the onPause () method
Step 3: you also need to add permissions to the AndroidManifest. xml file. During installation, the system will prompt whether to allow
Disable sleep! The permission is as follows:
<uses-permission android:name="android.permission.WAKE_LOCK" />
The simple code is as follows:
// Obtain the POWER_SERVICE object PowerManager pm = (PowerManager) getSystemService (Context. POWER_SERVICE); // use the newWakeLock () method to create a WakeLock instance PowerManager. wakeLock wl = pm. newWakeLock (PowerManager. SCREEN_DIM_WAKE_LOCK, "you can write a TAG here"); // it is best to put the onReusme method to call wl. acquire (); // preferably put it in the onPause method to call wl. release ();
The first parameter of newWakeLock has the following options:
PARTIAL_WAKE_LOCK: to keep the CPU running, the screen and keyboard lights may be disabled.
SCREEN_DIM_WAKE_LOCK: keeps the CPU running, allows screen display, but may be gray, allows the keyboard light to be turned off
SCREEN_BRIGHT_WAKE_LOCK: keeps the CPU running, allows screen highlighting, and allows you to turn off the keyboard light
FULL_WAKE_LOCK: keeps the CPU running, keeps the screen highlighted, and keeps the keyboard light bright.
② It is implemented directly through getWindow (). addFlags
This is relatively simple. To use the activity, add the following code in the onCreate () method:
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
There is no need to add permissions. Many of them are online. They only have to be set to always bright and don't talk about how to close them. It is increasingly found that Gu Ge is better than du Niang!
AddFlags () is used to add a flag. To clear the label, you only need to clear the label.
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); // click the button to clear the flag. btnclose disappears when the screen is always on. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View view) {getWindow (). clearFlags (WindowManager. layoutParams. FLAG_KEEP_SCREEN_ON );}});}}
3) use the keepScreenOn of the View component to make the screen always bright:
Previously, I introduced two methods to keep the screen always on. It seems that the question is incorrect. In this section, we will talk about the KeepScreenOn attribute;
But there's nothing to do. follow the same course to learn two more methods!It is very simple to use the keepScreenOn attribute.
Step 1: Set android: keepScreenOn = "true" to a UI component in the layout file corresponding to the Activity that needs to be normally highlighted"
Then, as long as the Activity is not stopped, the screen will always be on!
Step 2: how to disable the always-on mode? It's easy ~ Modify the attributes of the UI component and set it to false when you need to disable the usual brightness.
Compared with the previous two, is this more convenient? It's wise!
Also paste the following simple code:
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 = "close always bright"/> <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.txt liang); btnclose. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {if (flag =-1) {txtliang. setKeepScreenOn (false); txtliang. setText ("I cannot light up =-="); btnclose. setText ("bright");} else {txtliang. setKeepScreenOn (true); txtliang. setText ("I highlighted again ^-^"); btnclose. setText ("not bright ");}}});}}
4) last few words:
① The premise is that you do not press the power key ~~~ One press is definitely black screen, (* ^__ ^ *)...
② The code is relatively simple, so it will not be pasted. In addition, most of the above is the screen that is always on, so it will not be pasted.
Adjust the power-saving mode on your mobile phone, set the screen time-out to 15 s, then dry off, and test it on your own!