2

I have an android app which runs a foreground service and while that service is running I would like to give the user the option to keep the screen on.

I added to my settings preferences, a checkbox preference and if true, I would like to keep the screen on but have it set to off by default. It's just something my users have asked for. Currently i have the preference on but when I run the service my screen still shuts of here is what I've done

global variable

private static final String PREFS_DEVICE = "DeviceInfo";

code and if statement

SharedPreferences settings = getSharedPreferences(PREFS_DEVICE, 0);
            if(settings.getBoolean("screenPref", false)) {
                final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
                this.mWakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "My Tag");
                this.mWakeLock.acquire();
            }

and then i added this to my manifest

<uses-permission android:name="android.permission.WAKE_LOCK" />

Is there something I'm doing wrong. Is there a different way to do this from a service or is it just not possible from a service (hope that's not the case).

user577732
  • 3,956
  • 11
  • 55
  • 76
  • the application is running but the screen is off , that what u mean? – confucius Sep 08 '11 at 03:30
  • The process I'm running from a foreground service so it's running from the status bar but it's a long process so the screen often turns off and people have profiles to underclothes when the screen is off so while the service is on I want to give the option to keep the screen on so that the service isn't slowed by the screen turning off – user577732 Sep 08 '11 at 14:59

4 Answers4

4

You might use flag FLAG_KEEP_SCREEN_ON

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

and when you don't want to keep on

getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
Huynh NV
  • 41
  • 2
3

Try this:

mWakeLock = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.ON_AFTER_RELEASE,         
                            "");    
mWakeLock.acquire();
Farhana Haque
  • 1,371
  • 14
  • 23
  • where you release the wakelock.Your code looks ok. I have already implemented this in the same way in one of my application. may be problem is in your use. you might not use this in proper way. provide full code details where you want to use this. – Farhana Haque Sep 09 '11 at 03:56
  • Took my if statement out and just put this code in and it works fine thank you :) now to fix the if statement and what not – user577732 Sep 13 '11 at 17:20
1

This way is much better, bcz of using less permissions, services and ofc battery:

public void setWakelock(Activity Target, boolean State)
{
    if (State) Target.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    else Target.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

    return;
}

EDITED

Yousha Aleayoub
  • 4,532
  • 4
  • 53
  • 64
  • It's helpful to add some textual description to answers so that new users can better understand why your solution is the answer and not just copy and paste it. – Flexo Sep 14 '14 at 02:20
-1

Follow the pattern Mark Murphy provides with the WakefulIntentService. and you can find the explanation in his book very good one

confucius
  • 13,127
  • 10
  • 47
  • 66