I'd like to know how I can get the screen on without blocking while my app is open.
I'd like to know how I can get the screen on without blocking while my app is open.
The best way to do this is to use FLAG_KEEP_SCREEN_ON on your% (and only in a Activity
, never in a service or other application component). For example:
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 approach is that it does not require special permission, and the platform correctly manages the user moving between applications, without your application having to worry about releasing unused resources.
Another way to implement this is in the layout XML file of your application, using the android : keepScreenOn :
<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 Activity
is equivalent to using android:keepScreenOn="true"
.
You can use any approach that is best for your application.
The benefit of setting up programmatically on your FLAG_KEEP_SCREEN_ON
is that you
offers the option to turn off the screen via code.
Before setContentView () use:
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
It has a better way that would be an attribute in layout.xml but I do not remember right now