title: Android AlarmManager turn on screen when app in foreground
body: <p>I am creating an alarm app and therefore I want to run my app at a specific time using AlarmManager. It should turn the screen on and disable any keylock.</p>
  
  <p>Everything works fine if the app is closed or in the background. The AlarmMager calls my BroadcastReceiver and there I can start my activity. The screen goes on and I see my app.</p>
  
  <p>The problem is if my app is already in the foreground with screen turned off. I can hear the sound of my app so I know that the alarm has fired but the screen does not turn on.</p>
  
  <p>I have tried playing around with task and window settings but had no success. Intent.FLAG_ACTIVITY_NEW_TASK, </p>
  
  <p>The problem might be also related to the fact that I do not all window properties in the onCreate method. I do only want to turn the screen on if the app is deciding too that there is an alarm event and triggers startWakeup after onCreate.</p>
  
  <p>AndroidManifest.xml</p>
  
  <pre><code>&lt;uses-permission android:name="com.android.alarm.permission.SET_ALARM" /&gt;
      &lt;uses-permission android:name="android.permission.WAKE_LOCK" /&gt;
      &lt;uses-permission android:name="android.permission.DISABLE_KEYGUARD" /&gt;
      &lt;uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /&gt;
  
      &lt;application ...&gt;
      &lt;activity
              android:name=".FullscreenActivity"
              android:configChanges="orientation|keyboardHidden|screenSize"
              android:label="@string/app_name"
              android:theme="@style/Theme.AppCompat.NoActionBar"
              android:launchMode="singleTask"&gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.intent.action.MAIN" /&gt;
  
                  &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
              &lt;/intent-filter&gt;
          &lt;/activity&gt;
          &lt;receiver android:process=":remote" android:name=".AlarmMan"&gt;&lt;/receiver&gt;
          &lt;receiver android:name=".AutoStart"&gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.intent.action.BOOT_COMPLETED" /&gt;
              &lt;/intent-filter&gt;
          &lt;/receiver&gt;
          ...
          &lt;/activity&gt;
      &lt;/application&gt;
  </code></pre>
  
  <p>AlarmMan.java</p>
  
  <pre><code>public class AlarmMan extends BroadcastReceiver
  {
      @Override
      public void onReceive (Context context, Intent intent)
      {
          Intent i = new Intent(context, FullscreenActivity.class);
          i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
          context.startActivity(i);
      }
  }   
  </code></pre>
  
  <p>FullscreenActivity.java</p>
  
  <pre><code>public class FullscreenActivity extends AppCompatActivity {   
      @Override
      protected void onCreate(Bundle savedInstanceState) {
           getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED | 
          WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
          WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
      }
  
      public void startWakeup() {
  runOnUiThread(new Runnable() {
              @Override
              public void run() {
                  getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
                  mainFrame.setSystemUiVisibility(
                          View.SYSTEM_UI_FLAG_LOW_PROFILE |
                                  View.SYSTEM_UI_FLAG_FULLSCREEN |
                                  View.SYSTEM_UI_FLAG_LAYOUT_STABLE |
                                  View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY |
                                  View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION |
                                  View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                  );
                  }
                  });
                  }
  
                  public void stopWakeup() {
                  runOnUiThread(new Runnable() {
              @Override
              public void run() {
                 getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
  
                  mainFrame.setSystemUiVisibility(
                          View.SYSTEM_UI_FLAG_VISIBLE
                          );
                }
          });
      }
  </code></pre>