Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Start activity when screen is off

I have set up an AlarmManager to start up an activity. This activity also plays a sound, similar to an alarm app or an incoming call.

It works ok if the screen is on, even if the screen is locked.

If the screen is off, it doesn't work at all. I tried using the following as the first thing in onCreate

getWindow().setFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON,  WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);

If the screenlock is not enabled, this turns on the screen and I can see my activity closing. I can't hear the sound playing. If the screenlock is enabled, the screen won't turn on at all.

Sometimes I get the following, but not always:

07-18 23:52:13.685: E/OpenGLRenderer(14148):   GL_INVALID_OPERATION

How can I make it start properly when the screen is off?

like image 597
KKO Avatar asked Jul 18 '14 22:07

KKO


2 Answers

I got my answer partially from here.

        lock = ((KeyguardManager) getSystemService(Activity.KEYGUARD_SERVICE)).newKeyguardLock(KEYGUARD_SERVICE);
        powerManager = ((PowerManager) getSystemService(Context.POWER_SERVICE));
        wake = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "TAG");

        lock.disableKeyguard();
        wake.acquire();

        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);
like image 98
KKO Avatar answered Sep 20 '22 17:09

KKO


A while back I read that your app must be in full screen for the FLAG_TURN_SCREEN_ON to work.

"** One important note. Your activity must be full screen in order for the above flag combination to work. In my app I tried to use these flags with an activity which is not full screen (Dialog Theme) and it didn't work. After looking at the documentation I found that these flags require the window to be a full screen window." -Wake Android Device up

Quote from someone who posted their about a similar issue with FLAG_X.

like image 42
just.Blaise Avatar answered Sep 22 '22 17:09

just.Blaise