4

I have a streaming app and I want to avoid that the screen turns off while streaming. It works on most of my devices but it doesn't on certain Samsung devices (Samsung Galaxy S21 Ultra, Samsung Galaxy S10 etc). What I tried so far:

  1. Add this to the onCreate view of my activity

     this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    
  2. Add the keepScreenOn on the root component of my layout xml

     android:keepScreenOn="true"
    
  3. Use Power manager to prevent the screen from turning off in my onCreate

     PowerManager powerManager = (PowerManager) this.getSystemService(Context.POWER_SERVICE);
     this.wakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, "test.");
     this.wakeLock.acquire();
    

Doesn't matter what I do, Samsung screen lock will show up after a while and it will look like in the attached image below

enter image description here

Permissions like WAKE_LOCK are added in the Manifest of course. It seems so simple but I don't know how to avoid this. I constantly get complaints from users who are using my app on Samsung devices that the screen keeps turning off when they are using a gamepad for example and not actively touching the screen. How can this be avoided on Samsung devices? It seems works fine on all other Devices. I also checked a lot of stack overflow entries before and didn't find anything specific but maybe I missed something. Can anyone point me in the right direction?

the_dani
  • 2,466
  • 2
  • 20
  • 46
grill2010
  • 574
  • 6
  • 23
  • have you added `WAKE_LOCK` permission in manifest? – Rajasekaran M Feb 22 '21 at 12:42
  • 1
    Yes, sure I have. Should state that in my question :) – grill2010 Feb 22 '21 at 12:43
  • 1
    I incidentally found this: https://dontkillmyapp.com/samsung. '''UPDATE 2021: Despite Android team promise to enforce OEMs to be transparent about non-standard app killing, in Android 11 Samsung has introduced a new severe (default ON) restriction. Apps can no longer hold wake lock in foreground services. This breaks many use-cases, for instance health apps are now unable to gather sensoric data for their users. See details here and read below for workarounds.''' – the_dani Feb 22 '21 at 12:51
  • WTF, really Samsung? I thought already that something like this would be the case as it doesn't feel normal to me when it works on all my other 15 test devices... I will check it out, thanks. If there is really no workaround available, and it seems like that, feel free to post your comment as answer so that I can accept it. Extremely disappointing that there is not any other workaround then forcing the user to manually go to the settings. – grill2010 Feb 22 '21 at 12:59
  • 1
    Yess.. Samsung uses some battery optimization techniques. (Battery optimization is ON by default) The user will have to remove your app from the **Put to sleep** category. Sadly, the user will have to do that after every single update.. (which also downloads some crappy unwanted apps). Maybe you can seek permissions from user to remove the app from sleep settings... – Vidya Marathe Feb 22 '21 at 13:12
  • 1
    @the_dani tried to follow the instructions on the website but even then it seems it's not working. The lock screen will always show up after a while on my Samsung Galaxy S21 Ultra. Nevertheless your answer seems to be the most useful one and it could probably help others. If you could post your comment as answer I could provide you the bounty as the current answer doesn't help and I want to provide the bounty to you. The error is for sure because of some hidden Samsung battery optimization feature. – grill2010 Feb 27 '21 at 11:44

2 Answers2

1

I incidentally found this: https://dontkillmyapp.com/samsung

UPDATE 2021: Despite Android team promise to enforce OEMs to be transparent about non-standard app killing, in Android 11 Samsung has introduced a new severe (default ON) restriction. Apps can no longer hold wake lock in foreground services. This breaks many use-cases, for instance health apps are now unable to gather sensoric data for their users. See details here and read below for workarounds.

the_dani
  • 2,466
  • 2
  • 20
  • 46
  • 1
    As it seems there is currently no other answers and your answer is the most helpful one I will mark it as accepted. Thanks – grill2010 Aug 09 '21 at 14:48
0

You can do that with this code below:

PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
        "MyApp::MyWakelockTag");
wakeLock.acquire();

More informations: Android Documentation