0

I am trying to build an app which will prevent the phone screen from going off(even if the app is minimized to background)

I know that this feature is definitely possible as there are many apps in Play Store that does the same.

https://play.google.com/store/apps/details?id=com.eonsoft.ScreenON

This particular app prevents phone going to timeout for an infinite period of time. They ask for Draw over Other Apps Permission. But I am not able to figure out how it can help preventing phone from screen timeout. Can any of you experts guide me how I can achieve this?

Things I have already Tried:

getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

But the above code prevents phone from screen timeout only if my app is opened. I want this to work even if my is minimized.

I have tried the solutions given here: Android disable screen timeout while app is running

But the above solutions work only if my app is running. Let me explain with a use case scenario. I want the screen from going to timeout when they are reading a document using a PDF Viewer of their choice(in this case my app may not be active as they have minimized it )

SK707
  • 381
  • 1
  • 3
  • 8
  • "But the above code prevents phone from screen timeout only if my app is opened" -- no, it prevents the phone from screen timeout so long as the `Window` (from `getWindow()`) is visible. "They ask for Draw over Other Apps Permission" -- most likely, they are displaying their own `Window` from a `Service`. Note that this permission is not available on all Android devices and seems likely to go away in the coming years. – CommonsWare Sep 05 '21 at 12:57

1 Answers1

-1

You can run a service in the background and acquire lock like this :

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

and return onStartCommand START_STICKY. To release the wake lock, call wakelock.release(). Do not forget to put the permission in the manifest file :

<uses-permission android:name="android.permission.WAKE_LOCK" />
Hossein
  • 439
  • 3
  • 9