How to keep the screen turned on in later versions of the Kitkat API?

Asked

Viewed 170 times

2

I’m making a APP that keeps the smartphone screen always on, even when the APP is minimized.

I’m using a class with extends Service to do this, in onCreate I put the code that keeps the screen always lit:

PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
    wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "watever");
    wl.acquire();

Android Studio gives me an alert: FULL_WAKE_LOCK is deprecated, but I couldn’t figure out how to do it in the current form.

This code is only working until the Kitkat.
In the Lollipop or later, it does not present any error, however it does not work.

Example of app that makes screen stay lit even with the app closed: https://play.google.com/store/apps/details?id=com.brilliant.apps.screenon

  • 1

    I was curious because I need to keep the screen on from inside a Service, would you really need to? Just keep the CPU active using a PARTIAL_WAKE_LOCK would not solve?

  • The goal is to make the user leave the smartphone screen always on, using this feature with the app minimized or closed. If you know a form without using the Service it can also be. But it has to be able to use this feature with the APP minimized or closed.

  • With the Service, the user can use the screen on feature even when closing the APP, the apps that exist today in Google Play do like this for example: https://play.google.com/store/apps/details?id=com.brilliant.apps.screenon

1 answer

2

Currently you report this in the layout, either by XML or programmatically.

Programmatically would add that to onCreate:

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

in xml just insert in root layout the following.

android:keepScreenOn="true"
  • Thanks for the suggestion. But I’m doing everything in java, the user even minimizing the app, it remains active. Does not accept getWindow on Service onCreate.

  • Checking here, I used this function in an application here, it was to work correctly :(

  • 2

    The problem @Shogogan, is that he do it in a Service, but the ideal of this solution is to be made in a Activity, because of the Window.

  • 1

    understood now, pardon the misunderstanding of the question

  • My goal is to make the user leave the smartphone screen always on, even when it is out of the application. if you know a way without using the Service thank you. But he has to use this feature with the minimized APP. :)

  • looking at the documentation, they replaced the FULL_WAKE_LOCK for FLAG_KEEP_SCREEN_ON, I didn’t find a way to use it out, and now I’m curious too for if there is a way

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.