4

I am trying to make the screen to be always on while my test app is in the foreground with Ongoing Notification running. It works fine when the app is on resume state, but it doesn't work when I hit the home/middle button to put the app on pause state while showing up the Ongoing notification in the notification status bar.

Why getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON) does not work in the foreground/pause state?

MainActivity:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

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

    setContentView(R.layout.main);

    ...

    NotificationCompat.Builder OnGoingStatusBar = new NotificationCompat.Builder(this);
    OnGoingStatusBar.setSmallIcon(R.drawable.image);
    OnGoingStatusBar.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher));            
    OnGoingStatusBar.setTicker(Test);           
    OnGoingStatusBar.setContentTitle("Test");
    OnGoingStatusBar.setContentText("Testing Message");         
    OnGoingStatusBar.setWhen(System.currentTimeMillis());
    OnGoingStatusBar.setAutoCancel(false);
    OnGoingStatusBar.setContent(notificationView).build();
    OnGoingStatusBar.setOngoing(true);   //Create OnGoing Status Bar
    OnGoingStatusBar.setPriority(Notification.PRIORITY_MAX);
    NotificationManager.notify(STATUSBAR_ID, OnGoingStatusBar.build());
}
Kamran Ahmed
  • 7,661
  • 4
  • 30
  • 55
YRTM2014
  • 165
  • 1
  • 8
  • It only works when the app has a window, i.e. when the user can see its UI. – StoneBird Dec 09 '14 at 16:57
  • `getWindow()`. You have a window only when visible – njzk2 Dec 09 '14 at 16:58
  • thanks for the response. Is there any workaround that I can achieve my goal? I know this works on older Android version: pm = (PowerManager) getSystemService(Context.POWER_SERVICE); km = (KeyguardManager)getSystemService(Context.KEYGUARD_SERVICE); mKeyguardLock = km.newKeyguardLock("INFO"); mWakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP|PowerManager.ON_AFTER_RELEASE, "INFO"); mKeyguardLock.disableKeyguard();// dismiss the keyguard mWakeLock.acquire(); – YRTM2014 Dec 09 '14 at 18:58
  • ugg, the code doesn't look good in comment section... – YRTM2014 Dec 09 '14 at 18:59

1 Answers1

2

You should move getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON) below setContentView() and put android:keepScreenOn="true" into the root View of your main layout .

The documentation on this topic might be usefull: https://developer.android.com/training/scheduling/wakelock.html#screen

Rolf ツ
  • 8,611
  • 6
  • 47
  • 72
hungkk
  • 338
  • 3
  • 11
  • Thanks for the comment. I followed your suggestion, but there is no change. The screen still turns off whenever the activity is not visible / foreground/onPause state. Thanks – YRTM2014 Dec 11 '14 at 19:09
  • @YRTM2014 You are describing the intended functionality. See https://developer.android.com/training/scheduling/wakelock.html – Abandoned Cart Aug 10 '16 at 08:03
  • 1
    KeepScreenOn doesnt actually work for me, it still goes to sleep. – Ted Oct 22 '22 at 10:12