KKO
KKO

Reputation: 1933

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?

Upvotes: 6

Views: 5176

Answers (3)

KKO
KKO

Reputation: 1933

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);

Upvotes: 7

johng
johng

Reputation: 1026

Look into running a service, activity is going to be stopped when not in foreground.

Also look into the Activity lifecycle. http://developer.android.com/reference/android/app/Activity.html

Upvotes: 1

just.Blaise
just.Blaise

Reputation: 56

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.

Upvotes: 2

Related Questions