title: Android Keep Screen On In App
body: <p>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.</p>
  
  <p>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</p>
  
  <p><strong>global variable</strong></p>
  
  <pre><code>private static final String PREFS_DEVICE = "DeviceInfo";
  </code></pre>
  
  <p><strong>code and if statement</strong></p>
  
  <pre><code>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();
              }
  </code></pre>
  
  <p><strong>and then i added this to my manifest</strong></p>
  
  <pre><code>&lt;uses-permission android:name="android.permission.WAKE_LOCK" /&gt;
  </code></pre>
  
  <p>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).</p>