0

I want to wake up the device and play a video. Here is my code snippet to wake up and disable key guard.

pm = (PowerManager) getApplicationContext().getSystemService(
    Context.POWER_SERVICE);
keyguardManager = (KeyguardManager) getApplicationContext()
    .getSystemService(Context.KEYGUARD_SERVICE);
KeyguardLock keyguardLock = keyguardManager.newKeyguardLock("TAG");
wakeLock = pm.newWakeLock(
        (PowerManager.SCREEN_BRIGHT_WAKE_LOCK
            | PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP),
        "TAG");
wakeLock.acquire();
keyguardLock.disableKeyguard();
playVideo(getApplicationContext());
wakeLock.release();

The video is playing but the device is not waking up.

Any suggestions?

EDITED
I have found a working solution and I have posted it as an answer.
If there are better solutions, I would like to learn and know of it.
Thank you.

vidulaJ
  • 1,232
  • 1
  • 16
  • 31
  • "The video is playing but the device is not waking up" ??? – Mr_and_Mrs_D Jun 14 '14 at 09:58
  • Yes Mr_and_Mrs_D. The screen is off but the video is playing in the background. – vidulaJ Jun 16 '14 at 03:24
  • Where is this code snipet ? In a service ? – Mr_and_Mrs_D Jun 16 '14 at 09:32
  • I implemented a BroadcastReceiver inside the activity (LoginActivity), which I want the video activity (ScreenSaverActivity) to be called when screen is going to sleep. Inside the ScreenSaverActivity, I had the above code snippet. I think it's a mistake to put wakelock.release together with wakelock.aquire. Then I tried adding these three lines to ScreenSaverActivity, eventually it worked; WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON. Thank you Mr_and_Mrs_D for your answers and time. – vidulaJ Jun 17 '14 at 03:30
  • Welcome :) - please post your comment as an answer for the future generations to benefit from and then delete the comment. – Mr_and_Mrs_D Jun 17 '14 at 10:19
  • I will Mr_and_Mrs_D. Thank you. – vidulaJ Jun 17 '14 at 10:37

1 Answers1

0

After some referring and trouble I have found a working solution.

I implemented a BroadcastReceiver inside the activity (LoginActivity), which I want the video activity (ScreenSaverActivity) to be called when screen is going to sleep. Inside the onCreate method of ScreenSaverActivity, I had the above code snippet as mentioned in the question.
I think it's a mistake to put wakelock.release together with wakelock.aquire. Then I tried adding these three lines to ScreenSaverActivity and changed the above code snippet as follows, which eventually worked;

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

    pm = (PowerManager) getApplicationContext().getSystemService(Context.POWER_SERVICE);     

    keyguardManager = (KeyguardManager) getApplicationContext().getSystemService(Context.KEYGUARD_SERVICE); 
    keyguardLock    =  keyguardManager.newKeyguardLock("TAG");

    wakeLock = pm.newWakeLock((PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP), "TAG");
    wakeLock.acquire();

    keyguardLock.disableKeyguard(); 

    playVideo();


And I put,
keyguardLock.reenableKeyguard();
wakeLock.release();
after stopping video and before going back to previous (LoginActivity) activity.
Is there any better answer. I would like to learn and know of it.

vidulaJ
  • 1,232
  • 1
  • 16
  • 31