1

I have an activity that is suppose to wake the screen up at a certain time and open up the YouTube app, I can turn the screen on, but if I add a startActivity on the YouTube app it doesn't turn on(the YouTube app starts but the screen stays off).

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
                       | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
                       | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
                       | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
           |WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON);

    String path = "https://www.youtube.com/watch?v=63pKwVE4Uog";
    Uri uri = Uri.parse(path);
    uri = Uri.parse("vnd.youtube:" + uri.getQueryParameter("v"));
    Intent i= new Intent(Intent.ACTION_VIEW, uri);
    i.addFlags(FLAG_ACTIVITY_NEW_TASK);
    startActivity(i);
}

if I just run the code to turn on the screen - it does, if I just run the code to start the YouTube app - it does, if I put both, the screen doesn't turn on but the YouTube app starts. I'm guessing because once YouTube app starts, the activity that kept the screen on is destroyed/paused - in which case the screen turns off(even if I can't see it turning on).

Am I right about what the problem is? if so how can this be done?

  • Try to add a handler to delay the start of YouTube app. By that way your screen will turn on and stay awake when the YouTube app launches – Jonathan Aste Apr 03 '17 at 17:56

1 Answers1

0

As a workarround, as I said on my comment, you can add a handler to delay the start of YouTube App.

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

   getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
                   | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
                   | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
                   | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
       |WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON);

    String path = "https://www.youtube.com/watch?v=63pKwVE4Uog";
    Uri uri = Uri.parse(path);
    uri = Uri.parse("vnd.youtube:" + uri.getQueryParameter("v"));
    Intent i= new Intent(Intent.ACTION_VIEW, uri);
    i.addFlags(FLAG_ACTIVITY_NEW_TASK);
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            startActivity(i);
        }
    }, 2000);
}
Jonathan Aste
  • 1,764
  • 1
  • 13
  • 20