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