In my app, I need to make sure the android device's screen stays on when the user clicks a button. When the use clicks another button, I want to allow the screen to turn off when it normally would. To do this, I need to call:
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
The android documentation states that this call must be made from an activity, which is what I've done. Here is my code snippet:
public class AndroidDataProvider implements DataProvider {
@Override
public void keepScreenOn(boolean flag) {
if(flag) {
Window window = FXActivity.getInstance().getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
else {
Window window = FXActivity.getInstance().getWindow();
window.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
}
}
When I run this code on my Samsung Galaxy S5 and S6, I get an exception. The code to keep the screen on works when I run it natively in android studio, so that's not the issue. Any idea how to get this functionality to work? wake lock will not work because I need to enable and disable this functionality based on UI events.