Keeping Screen on

When Android developer wants to keep screen on while application is running, it often ends up with him managing wake locks. Quite often this is not needed.

If only thing you want to do is to keep screen on while your application is running (e.g. video player) and you do not need more advanced power management control, you should just add another flag to existing window.

@Override 
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
setContentView(...);
...2
}

System itself will now ensure that screen is kept turned on while your application is visible. As soon as your application is in background, everything returns back to normal. Added benefit of this approach is that no additional permissions are required.

One thought to “Keeping Screen on”

Leave a Reply to AlliXSenoS Cancel reply

Your email address will not be published. Required fields are marked *