When an Android device is idle, first its screen will darken, then turn off the screen and finally turn off the CPU. This prevents the device's power from being consumed quickly. Sometimes, however, there are some exceptions:
Apps such as games or movie apps could need to keep, the screen turned on. For example: Users need to keep the screen solid while playing the game
Other applications need the screen to remain on, but they may require the CPU to keep running until a critical ope Ration finishes. Another example: Some specific applications do not require the screen to remain constant, but the CPU continues to run until some logical execution ends
The following topics discuss how to keep your device awake when necessary without consuming too much power.
Topic One: Keep Device awake State
To avoid draining the battery, a Android device that's left idle quickly falls asleep. However, there is times when a application needs to wake up the screens or the CPU and keep it awake to complete some wor K. In order to avoid excessive power consumption, Android devices will quickly go to sleep after being idle. However, sometimes applications will need to wake up the screen or wake up the CPU and keep them awake until some tasks are completed.
This needs to be done on a case-by-case basis, but the most lightweight approach must be used to avoid any impact on system resources.
How do I keep the screen solid?
Some apps need to keep the screen lit up, such as gaming and video applications. The best way to do this is to use the flag_keep_screen_on attribute in your activity (and only in activity, not in service or other application components).
public class Mainactivity extends Activity { @Override protected void onCreate (Bundle savedinstancestate) { super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); GetWindow (). Addflags (WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); }
The advantage of this method differs from the wake-up lock (Wake lock), which does not require any special permissions, and the system correctly manages the switching between applications without having to issue a resource release.
Another way is to declare the Android:keepscreenon property in the layout file:
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "Match_ Parent " android:layout_height=" match_parent " android:keepscreenon=" true "> ... </ Relativelayout>
Using android:keepscreenon= "true" is equivalent to using FLAG_KEEP_SCRRE_ON. You can choose the method that best suits your application. The advantage of setting the steady-state identity through code in activity is that you can dynamically clear the indicator from the code so that the screen can be closed.
It is important to note that you do not need to clear the FLAG_KEEP_SCRRE_ON logo unless you no longer want the running app to light up the screen for a long time (for example, if you want to turn off the screen after a certain period of time no action has occurred). WindowManager will correctly manage the light or close of the screen when the app enters the background or returns to the foreground. But if you want to explicitly clear this identity, so that the screen can be turned off, you can use the GetWindow (). Clearflags (WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON) method.
Topic Two: Scheduling repetitive alarms
Manage wake-up status for Android devices