grill2010
grill2010

Reputation: 614

Android keep screen on on Samsung devices

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?

Upvotes: 5

Views: 2082

Answers (2)

the_dani
the_dani

Reputation: 2524

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.

Upvotes: 2

George Sepetadelis
George Sepetadelis

Reputation: 336

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

Upvotes: 0

Related Questions